# Auto detect the number of CPU's and set worker\_processes

**URL:** <https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944>\
**Category:** NGINX\
**Created:** [September 16, 2012, 1:56am UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944 "2012-09-16T01:56:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 16, 2012, 1:56am UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944/1 "2012-09-16T01:56:37Z")

</div>

I am trying to get fancy and have `nginx.conf` auto-detect the number of  
CPU’s on the server and then set `worker_processes` to that number. Here  
is  
what I have:

```
worker_processes $(cat /proc/cpuinfo | grep "processor" | sort -u | 

```

wc  
-l);

Unfortunately, that is throwing an error on nginx start. Is it even  
possible  
to run shell commands from inside nginx configuration files?

Thanks.

Posted at Nginx Forum:

> **[Auto detect the number of CPU's and set worker\_processes](https://forum.nginx.org/read.php?2,230757,230757#msg-230757)**
>
> I am trying to get fancy and have \`nginx.conf\` auto-detect the number of CPU's on the server and then set \`worker\_processes\` to that number. Here is what I have: worker\_processes $(cat /proc/cpuinfo | grep "processor" | sort -u | wc -l);...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [September 16, 2012, 5:49am UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944/2 "2012-09-16T05:49:05Z")

</div>

Hi,

On Sun, Sep 16, 2012 at 7:55 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

It’s not supported in current nginx.

But you may try out tengine, which is a nginx distribution and has the  
exact feature you want: [http://tengine.taobao.org/document/core.html](http://tengine.taobao.org/document/core.html)  
More features can be found at: [http://tengine.taobao.org/](http://tengine.taobao.org/)

Regards,

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [September 17, 2012, 11:44am UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944/3 "2012-09-17T11:44:13Z")

</div>

Hello!

On Sat, Sep 15, 2012 at 07:55:49PM -0400, justin wrote:

> I am trying to get fancy and have `nginx.conf` auto-detect the number of  
> CPU’s on the server and then set `worker_processes` to that number. Here is  
> what I have:
> 
> ```
> worker_processes $(cat /proc/cpuinfo | grep "processor" | sort -u | wc
> 
> ```
> 
> -l);
> 
> Unfortunately, that is throwing an error on nginx start. Is it even possible  
> to run shell commands from inside nginx configuration files?

No, it’s not currently supported. You may try e.g. make + sed (or  
any other templating system) to do this, as well as many other  
macro-expansion related things.

Maxim D.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [September 17, 2012, 1:35pm UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944/4 "2012-09-17T13:35:15Z")

</div>

On Sat, 15 Sep 2012 19:55:49 -0400, “justin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am trying to get fancy and have `nginx.conf` auto-detect

Not knowing how to auto-detect it, I usually just excute this as ROOT:

sed -i ‘/^worker\_processes/s,[0-9]+,’“$(sort -u /proc/cpuinfo | grep  
-c processor)”‘,’ /etc/nginx/nginx.conf

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [September 17, 2012, 11:56am UTC](https://www.ruby-forum.com/t/auto-detect-the-number-of-cpus-and-set-worker-processes/222944/5 "2012-09-17T11:56:30Z")

</div>

On Sep 16, 2012, at 3:55 , justin wrote:

> I am trying to get fancy and have `nginx.conf` auto-detect the number of  
> CPU’s on the server and then set `worker_processes` to that number. Here is  
> what I have:
> 
> worker\_processes $(cat /proc/cpuinfo | grep “processor” | sort -u | wc  
> -l);
> 
> Unfortunately, that is throwing an error on nginx start. Is it even possible  
> to run shell commands from inside nginx configuration files?

You can configure it via command line:  
nginx -g “worker\_processes `cat /proc/cpuinfo | grep "processor" | sort -u | wc -l`;”

–  
Igor S.

> **[Technical Support for NGINX and NGINX Plus Software](https://www.nginx.com/support/)**
>
> Get the help you need with support directly from the authors and maintainers of NGINX Open Source and NGINX Plus.
