Check running cron processes with php - php

I have the following result when I do a
ps ax
13790 pts/0 S 0:00 /usr/bin/php cron3.php 0
13804 pts/0 S 0:00 /usr/bin/php cron3.php 1
13819 pts/0 S 0:00 /usr/bin/php cron3.php 2
13834 pts/0 S 0:00 /usr/bin/php cron3.php 3
13851 pts/0 S 0:00 /usr/bin/php cron3.php 4
13863 pts/0 S 0:00 /usr/bin/php cron3.php 5
13879 pts/0 S 0:00 /usr/bin/php cron3.php 6
13897 pts/0 S 0:00 /usr/bin/php cron3.php 7
13913 pts/0 S 0:00 /usr/bin/php cron3.php 8
13927 pts/0 S 0:00 /usr/bin/php cron3.php 9
13941 pts/0 S 0:00 /usr/bin/php cron3.php 10
13958 pts/0 S 0:00 /usr/bin/php cron3.php 11
13969 pts/0 S 0:00 /usr/bin/php cron3.php 12
13983 pts/0 S 0:00 /usr/bin/php cron3.php 13
13996 pts/0 S 0:00 /usr/bin/php cron3.php 14
14012 pts/0 S 0:00 /usr/bin/php cron3.php 15
14026 pts/0 S 0:00 /usr/bin/php cron3.php 16
14039 pts/0 S 0:00 /usr/bin/php cron3.php 17
14052 pts/0 S 0:00 /usr/bin/php cron3.php 18
14067 pts/0 S 0:00 /usr/bin/php cron3.php 19
14083 pts/0 S 0:00 /usr/bin/php cron3.php 20
14095 pts/0 S 0:00 /usr/bin/php cron3.php 0
14113 pts/0 S 0:00 /usr/bin/php cron3.php 1
I want to able to only ensure that at 1 time, only 1 cron3.php [argv] can be run.
Meaning if
cron3.php 1
is running in the process, I need to able know that its running by a php line or something to check.
I tried googling and the closest I found is
exec("ps auxwww|grep cron3.php|grep -v grep", $output);
However If i change it to
exec("ps auxwww|grep "cron3.php 1"|grep -v grep", $output);
It list me all that contain 1, including the process like 10, 11 , 12 etc.
Thanks

Anchor it to the end of the line:
exec("ps auxwww | grep '[c]ron3.php 1$'", $output);
Using [c]ron3 is a trick to avoid having to use a second grep -v grep, since that matches cron3, but it won't match itself.

Place a $ at the end of grep pattern:
grep "cron3.php 1$"
In regular expression $ means "end of text".

Related

reload `procmgr` in container?

So I have a docker container build with buildpack that runs the following command under PID 1
procmgr /layers/paketo-buildpacks_php-web/php-web/procs.yml
is it possible to reload the configs passed into procmgr somehow?
🐳 [DEV] backend-v1-7476cc6cfd-l68p2 app #
ps aufx
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
cnb 30 1.0 0.0 18648 3444 pts/0 Ss 03:22 0:00 bash
cnb 77 0.0 0.0 34412 2944 pts/0 R+ 03:22 0:00 \_ ps aufx
cnb 1 0.0 0.1 1013616 4096 ? Ssl 03:20 0:00 procmgr /layers/paketo-buildpacks_php-web/php-web/procs.yml
cnb 24 0.0 0.7 323596 31260 ? Ss 03:20 0:00 php-fpm: master process (/layers/paketo-buildpacks_php-web/php-web/etc/php-fpm.conf)
cnb 28 0.3 0.6 330252 24112 ? S 03:20 0:00 \_ php-fpm: pool www
cnb 29 0.1 0.6 330016 23900 ? S 03:20 0:00 \_ php-fpm: pool www
cnb 25 0.0 0.1 25132 5648 ? S 03:20 0:00 nginx: master process nginx -p /workspace/app -c /workspace/app/nginx.conf
cnb 26 0.0 0.0 25132 1988 ? S 03:20 0:00 \_ nginx: worker process
cnb 27 0.0 0.0 25132 1988 ? S 03:20 0:00 \_ nginx: worker process
🐳 [DEV] backend-v1-7476cc6cfd-l68p2 app #
cat /layers/paketo-buildpacks_php-web/php-web/procs.yml
processes:
nginx:
command: nginx
args:
- -p
- /workspace/app
- -c
- /workspace/app/nginx.conf
php-fpm:
command: php-fpm
args:
- -p
- /layers/paketo-buildpacks_php-web/php-web
- -y
- /layers/paketo-buildpacks_php-web/php-web/etc/php-fpm.conf
- -c
- /layers/paketo-buildpacks_php-web/php-web/etc
At the time of writing this, no. The procmgr cli that is used here is very basic.
https://github.com/paketo-buildpacks/php-web/blob/main/cmd/procmgr/main.go
It loads the processes information listed in procs.yml, starts those processes, redirects all STDOUT/STDERR, and watches for a process to exit. If one process exits, then they all exit. There's no reload capability.
If you need to reload the information in procs.yml, you'll need to reload your container.

Supervisor is not started into my docker container (Laravel project)

Supervisor is not started into my container and i am not able to run my php artisan queue:work command for my laravel project.
Extract from my Dockerfile
# Add worker to supervisor config file
COPY laravel-worker.conf /etc/supervisor/conf.d/
CMD ["/usr/bin/supervisord"]
Here is the laravel-worker.conf:
[program:laravel-worker]
command=php /var/www/test/current/artisan queue:work --tries=3
user=myuser
process_name=%(program_name)s_%(process_num)d
directory=/var/www/test/current
stdout_logfile=/tmp/supervisord.log
redirect_stderr=true
numprocs=1
autostart=true
autorestart=true
When i go into the container, the supervisor service is not started:
root#e7227ef40f63:/# service supervisor status
supervisord is not running.
And process are following:
root#e7227ef40f63:/# ps -aux | grep supervisor
root 1 0.0 0.0 4328 652 ? Ss 18:21 0:00 /bin/sh -c service ssh restart && service apache2 restart && service cron start && bash /usr/bin/supervisord
root 365 0.0 0.0 55808 10632 ? Ss 18:25 0:00 /usr/bin/python /usr/bin/supervisord
root 380 0.0 0.0 11120 712 ? S+ 18:27 0:00 grep supervisor
UPDATE
I edited my DockerFile and put this line:
ENTRYPOINT service ssh restart && service apache2 restart && service cron start && /usr/bin/supervisord && bash
The service is now well started when the container starts :
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.0 4328 652 ? Ss 05:20 0:00 /bin/sh -c service ssh restart && service apache2 restart && service cron start && /usr/bin/supervisord && bash
root 25 0.0 0.0 55176 1140 ? Ss 05:20 0:00 /usr/sbin/sshd
root 43 0.1 0.0 406408 25504 ? Ss 05:20 0:00 /usr/sbin/apache2 -k start
www-data 46 0.0 0.0 406440 8416 ? S 05:20 0:00 /usr/sbin/apache2 -k start
www-data 47 0.0 0.0 406440 8416 ? S 05:20 0:00 /usr/sbin/apache2 -k start
www-data 48 0.0 0.0 406440 8416 ? S 05:20 0:00 /usr/sbin/apache2 -k start
www-data 49 0.0 0.0 406440 8416 ? S 05:20 0:00 /usr/sbin/apache2 -k start
www-data 50 0.0 0.0 406440 8416 ? S 05:20 0:00 /usr/sbin/apache2 -k start
root 59 0.0 0.0 17484 636 ? Ss 05:20 0:00 /usr/sbin/cron
root 63 0.2 0.0 56012 10788 ? Ss 05:20 0:00 /usr/bin/python /usr/bin/supervisord
root 64 0.0 0.0 20032 1280 ? S 05:20 0:00 bash
root 89 0.1 0.0 20240 1996 ? Ss 05:20 0:00 bash
root 112 0.0 0.0 17492 1168 ? R+ 05:21 0:00 ps -aux
But it seams that supervisor doesn't start my config file because i don't see the 8 processes that should be run..
This is the bad part:
&& bash /usr/bin/supervisord
supervisord is not a bash script. Execute it as is: && /usr/bin/supervisord.
However, I recommend you to completely avoid using service in a container. In general, running more than one process into a container is considered an antipattern, but if you really need it, better only use supervisor . Create a .conf file for each process (cron, sshd, etc) and only run supervosord as is in your CMD.

Digitalocean's wordpress one-click install stops continously

I have a wordpress site hosted on a digitalocean's droplet and installed through their one-click installation that goes down very often.
Here the url: sinc.marchespettacolo.it
Almost once a week i have to restart Apache from the console because the site is down and if i try to connect to it i get a timeout.
I'm not that pro on making a diagnosis on what is causing this error.
Could someone help me how i have to proceed to find out what's causing this situation and how i can find a solution to the problem?
I have also another droplet with another droplet where i've installed wordpress with the same process that gives no problems!
Some says could be a problem connected to Apache opening to many processes.
This is the last process log, done while the site was down.
root#sinc:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 33460 980 ? Ss 04:27 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 04:27 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 04:27 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 04:27 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 04:27 0:12 [rcu_sched]
root 8 0.0 0.0 0 0 ? R 04:27 0:13 [rcuos/0]
root 9 0.0 0.0 0 0 ? S 04:27 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S 04:27 0:00 [rcuob/0]
root 11 0.0 0.0 0 0 ? S 04:27 0:00 [migration/0]
root 12 0.0 0.0 0 0 ? S 04:27 0:00 [watchdog/0]
root 13 0.0 0.0 0 0 ? S< 04:27 0:00 [khelper]
root 14 0.0 0.0 0 0 ? S 04:27 0:00 [kdevtmpfs]
root 15 0.0 0.0 0 0 ? S< 04:27 0:00 [netns]
root 16 0.0 0.0 0 0 ? S< 04:27 0:00 [writeback]
root 17 0.0 0.0 0 0 ? S< 04:27 0:00 [kintegrityd]
root 18 0.0 0.0 0 0 ? S< 04:27 0:00 [bioset]
root 19 0.0 0.0 0 0 ? S< 04:27 0:00 [kworker/u3:0]
root 20 0.0 0.0 0 0 ? S< 04:27 0:00 [kblockd]
root 21 0.0 0.0 0 0 ? S< 04:27 0:00 [ata_sff]
root 22 0.0 0.0 0 0 ? S 04:27 0:00 [khubd]
root 23 0.0 0.0 0 0 ? S< 04:27 0:00 [md]
root 24 0.0 0.0 0 0 ? S< 04:27 0:00 [devfreq_wq]
root 25 0.0 0.0 0 0 ? S 04:27 0:05 [kworker/0:1]
root 27 0.0 0.0 0 0 ? S 04:27 0:00 [khungtaskd]
root 28 3.5 0.0 0 0 ? S 04:27 18:14 [kswapd0]
root 29 0.0 0.0 0 0 ? SN 04:27 0:00 [ksmd]
root 30 0.0 0.0 0 0 ? SN 04:27 0:00 [khugepaged]
root 31 0.0 0.0 0 0 ? S 04:27 0:00 [fsnotify_mark]
root 32 0.0 0.0 0 0 ? S 04:27 0:00 [ecryptfs-kthrea]
root 33 0.0 0.0 0 0 ? S< 04:27 0:00 [crypto]
root 45 0.0 0.0 0 0 ? S< 04:27 0:00 [kthrotld]
root 47 0.0 0.0 0 0 ? S 04:27 0:00 [vballoon]
root 48 0.0 0.0 0 0 ? S 04:27 0:00 [scsi_eh_0]
root 49 0.0 0.0 0 0 ? S 04:27 0:00 [scsi_eh_1]
root 70 0.0 0.0 0 0 ? S< 04:27 0:00 [deferwq]
root 71 0.0 0.0 0 0 ? S< 04:27 0:00 [charger_manager]
root 116 0.0 0.0 0 0 ? S 04:27 0:00 [scsi_eh_2]
root 118 0.0 0.0 0 0 ? S< 04:27 0:00 [kpsmoused]
root 119 0.0 0.0 0 0 ? S 04:27 0:00 [kworker/0:2]
root 126 0.0 0.0 0 0 ? S 04:27 0:00 [jbd2/vda1-8]
root 127 0.0 0.0 0 0 ? S< 04:27 0:00 [ext4-rsv-conver]
root 313 0.0 0.0 19476 0 ? S 04:27 0:00 upstart-udev-bridge --daemon
root 323 0.0 0.0 51340 128 ? Ss 04:27 0:00 /lib/systemd/systemd-udevd --daemon
message+ 326 0.0 0.0 39228 268 ? Ss 04:27 0:00 dbus-daemon --system --fork
root 392 0.0 0.0 43452 560 ? Ss 04:27 0:00 /lib/systemd/systemd-logind
syslog 396 0.0 0.0 255844 360 ? Ssl 04:27 0:01 rsyslogd
root 430 0.0 0.0 15408 4 ? S 04:27 0:00 upstart-file-bridge --daemon
root 542 0.0 0.0 0 0 ? S< 04:27 0:00 [ttm_swap]
root 635 0.0 0.0 0 0 ? S< 04:27 0:00 [kvm-irqfd-clean]
root 771 0.0 0.0 15656 4 ? S 04:27 0:00 upstart-socket-bridge --daemon
root 791 0.0 0.0 15820 116 tty4 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty4
root 795 0.0 0.0 15820 116 tty5 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty5
root 800 0.0 0.0 15820 116 tty2 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty2
root 801 0.0 0.0 15820 116 tty3 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty3
root 804 0.0 0.0 15820 116 tty6 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty6
root 837 0.0 0.0 61364 272 ? Ss 04:27 0:01 /usr/sbin/sshd -D
root 841 0.0 0.0 4368 96 ? Ss 04:27 0:00 acpid -c /etc/acpi/events -s /var/run/acpid.socket
root 843 0.0 0.0 23656 204 ? Ss 04:27 0:00 cron
daemon 844 0.0 0.0 19140 56 ? Ss 04:27 0:00 atd
whoopsie 868 0.0 0.0 335684 792 ? Ssl 04:27 0:00 whoopsie
mysql 916 0.1 2.5 918056 25668 ? Ssl 04:27 1:01 /usr/sbin/mysqld
root 1001 0.0 0.0 25344 376 ? Ss 04:27 0:00 /usr/lib/postfix/master
postfix 1006 0.0 0.0 27572 936 ? S 04:27 0:00 qmgr -l -t unix -u
root 1126 0.0 0.0 15820 116 tty1 Ss+ 04:27 0:00 /sbin/getty -8 38400 tty1
root 1384 0.0 0.0 0 0 ? S< 04:28 0:12 [kworker/u3:1]
root 1416 0.0 0.0 0 0 ? S 04:28 0:00 [kauditd]
root 2853 0.0 0.0 0 0 ? S 05:33 0:00 [kworker/u2:2]
root 3541 0.0 0.0 0 0 ? S 05:59 0:00 [kworker/u2:0]
postfix 6465 0.0 0.0 27408 968 ? S 12:48 0:00 pickup -l -t unix -u -c
root 6508 0.0 0.0 105632 596 ? Ss 12:58 0:00 sshd: root#pts/0
root 6578 0.0 0.1 22404 1484 pts/0 Ss 12:58 0:00 -bash
root 6639 0.0 0.0 0 0 ? S 13:00 0:00 [kworker/u2:1]
root 6673 0.0 1.5 312976 15964 ? Ss 13:00 0:00 /usr/sbin/apache2 -k start
www-data 6677 1.8 2.6 319132 27180 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6678 1.8 2.6 319132 27180 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6679 2.0 3.8 324792 38892 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6680 1.8 3.5 325320 35736 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6681 1.8 2.6 319132 27180 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6688 1.8 2.6 319132 27176 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6690 1.8 2.4 317344 25372 ? S 13:00 0:03 /usr/sbin/apache2 -k start
www-data 6691 1.7 2.6 319132 27176 ? S 13:00 0:02 /usr/sbin/apache2 -k start
root 6700 0.0 0.1 18448 1292 pts/0 R+ 13:03 0:00 ps aux
Any wonderfull idea?
Thaaaanks in advance.
UPDATE
05-Apr-2016
here the Apache error log as requested:
https://gist.github.com/iperdiscount/ff06cf131f7ac1ec31aa28761e36b1c9#file-gistfile1-txt

Check how many instances of a script are already running using linux ps command

I have a cron job that takes about 5 minutes to complete and it's set to run every minute.
I know that the script gets executed faster as it would terminate but that's intentionally.
I want to have about 5 instances of this script running at the same time but as it is now, the server will just run too many instances till everything freezes.
That's why I want to perform a check at the beginning of each cron how many instances are already running and to immediately terminate if there are more than 5 running already.
How could I do that most effectively?
Online I found this snippet which might be a good solution:
exec("ps auxwww|grep cleaner.php|grep -v grep", $output);
print_r($output);
while cleaner.php is the script in question.
If I execute this script, I get the following output:
Array
(
[0] => root 4565 0.0 0.0 6184 776 ? SNs 15:42 0:00 jailshell (user1) [4732] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[1] => user1 4732 0.0 0.0 6184 396 ? SN 15:42 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[2] => user1 4733 0.0 0.0 106056 1264 ? SN 15:42 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[3] => user1 4734 0.0 0.3 366452 15988 ? SN 15:42 0:00 php /home/user1/public_html/crons/cleaner.php
[4] => root 4776 0.0 0.0 6184 776 ? SNs 15:44 0:00 jailshell (user1) [4793] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[5] => user1 4793 0.0 0.0 6184 396 ? SN 15:44 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[6] => user1 4794 0.0 0.0 106056 1264 ? SN 15:44 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[7] => user1 4795 0.0 0.3 366452 16008 ? SN 15:44 0:00 php /home/user1/public_html/crons/cleaner.php
[8] => root 6111 0.0 0.0 6184 776 ? SNs 15:46 0:00 jailshell (user1) [6126] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[9] => user1 6126 0.0 0.0 6184 396 ? SN 15:46 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[10] => user1 6127 0.0 0.0 106056 1264 ? SN 15:46 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[11] => user1 6128 0.0 0.3 366452 15920 ? SN 15:46 0:00 php /home/user1/public_html/crons/cleaner.php
[12] => root 6218 0.0 0.0 6184 776 ? SNs 15:48 0:00 jailshell (user1) [7368] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[13] => user1 7368 0.0 0.0 6184 396 ? SN 15:48 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[14] => user1 7369 0.0 0.0 106056 1260 ? SN 15:48 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[15] => user1 7370 0.0 0.3 366452 15960 ? SN 15:48 0:00 php /home/user1/public_html/crons/cleaner.php
[16] => root 7462 0.0 0.0 6184 776 ? SNs 15:50 0:00 jailshell (user1) [7704] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[17] => user1 7704 0.0 0.0 6184 396 ? SN 15:50 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[18] => user1 7705 0.0 0.0 106056 1256 ? SN 15:50 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[19] => user1 7706 0.0 0.3 366452 15960 ? SN 15:50 0:00 php /home/user1/public_html/crons/cleaner.php
[20] => root 11969 0.0 0.0 6184 772 ? SNs 15:52 0:00 jailshell (user1) [11987] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[21] => user1 11987 0.0 0.0 6184 392 ? SN 15:52 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[22] => user1 11992 0.0 0.0 106056 1264 ? SN 15:52 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[23] => user1 11993 0.2 0.3 366452 15888 ? SN 15:52 0:00 php /home/user1/public_html/crons/cleaner.php
[24] => root 24158 0.0 0.0 6184 780 ? SNs 15:30 0:00 jailshell (user1) [24232] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[25] => user1 24232 0.0 0.0 6184 400 ? SN 15:30 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[26] => user1 24254 0.0 0.0 106056 1264 ? SN 15:30 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[27] => user1 24260 0.0 0.3 366448 15952 ? SN 15:30 0:00 php /home/user1/public_html/crons/cleaner.php
[28] => root 28649 0.0 0.0 6184 772 ? SNs 15:32 0:00 jailshell (user1) [29645] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[29] => user1 29645 0.0 0.0 6184 392 ? SN 15:32 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[30] => user1 29647 0.0 0.0 106056 1264 ? SN 15:32 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[31] => user1 29648 0.0 0.3 366452 15972 ? SN 15:32 0:00 php /home/user1/public_html/crons/cleaner.php
[32] => root 29821 0.0 0.0 6184 776 ? SNs 15:34 0:00 jailshell (user1) [29840] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[33] => user1 29840 0.0 0.0 6184 396 ? SN 15:34 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[34] => user1 29843 0.0 0.0 106056 1260 ? SN 15:34 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[35] => user1 29845 0.0 0.3 366452 15984 ? SN 15:34 0:00 php /home/user1/public_html/crons/cleaner.php
[36] => root 30055 0.0 0.0 6184 776 ? SNs 15:36 0:00 jailshell (user1) [30090] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[37] => user1 30090 0.0 0.0 6184 396 ? SN 15:36 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[38] => user1 30101 0.0 0.0 106056 1260 ? SN 15:36 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[39] => user1 30108 0.0 0.3 366452 15968 ? SN 15:36 0:00 php /home/user1/public_html/crons/cleaner.php
[40] => root 31311 0.0 0.0 6184 776 ? SNs 15:38 0:00 jailshell (user1) [31332] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[41] => user1 31332 0.0 0.0 6184 396 ? SN 15:38 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[42] => user1 31333 0.0 0.0 106056 1260 ? SN 15:38 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[43] => user1 31336 0.0 0.3 366452 15976 ? SN 15:38 0:00 php /home/user1/public_html/crons/cleaner.php
[44] => root 31610 0.0 0.0 6184 776 ? SNs 15:40 0:00 jailshell (user1) [31675] ll -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[45] => user1 31675 0.0 0.0 6184 396 ? SN 15:40 0:00 jailshell (user1) [init] ell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[46] => user1 31680 0.0 0.0 106056 1260 ? SN 15:40 0:00 /usr/local/cpanel/bin/jailshell -c php /home/user1/public_html/crons/cleaner.php >/dev/null 2>&1
[47] => user1 31685 0.0 0.3 366452 15972 ? SN 15:40 0:00 php /home/user1/public_html/crons/cleaner.php
)
Now I am a bit confused that some instances are run under user1 and some under root and also about the jailshell.
What line of the output from above means the script gets currently executed 1 time or is the script really being executed 48 times currently? I suppose more filtering is needed.
Is this even the best solution for what I try to achieve?
I don't have experience with cpanel/jailshell and using it to run cronjobs but I have tried to answer the question the best I can.
There is a pattern that emerges from the output you listed in that the start time of processes is clearly in groups of 4 which makes me think that each group of 4 is one instance of your cleaner running. A parent process is perhaps forking sub process. You can get the parent process by using “ps -ef” command to validate this theory.
If this theory is correct you could then use
exec("ps auxwww | grep cleaner.php | grep -v grep | grep -v jailshell”, $output);
which would return just return
php /home/user1/public_html/crons/cleaner.php
which should then give you the actual number of processes running if you used a count on $ouput.
In terms of implementation you could use a table in a database that stores a value with the current number of cleaners running and increment and decrement the value as crons stop and start. You would also need to do this with some form of transaction so updates to the table are atomic.
However the fact that the cron runs every minute and for a period of 5 minutes makes me think that you need to perhaps look at making cleaner.php more efficient so that you don't need 5 instances of it running. This then means you can remove the problem of only having 5 instances of the cleaner running at once.
With my comment on your original question it seems that perhaps cpanel/jailshell has duplicated crons as you got a result for both
crontab -u root -l and crontab -u user1 -l
so I would make sure that your cron first is only executed once and not by both the root and user1 users.
Short answer:
In WHM under "Tweak Settings" -> "System", option titled: "Jailed /proc mount method". If you set it to "Always mount full /proc", this will allow CRON launched scripts to see ALL of the processes via ps and top. If you keep jailshell enabled, you still have to ignore them (i.e. grep -v jailshell), but the counts will be accurate again.
Explanation and History:
This exact issue has been plaguing me for years! I have numerous scripts, across several of my sites that CRON runs every minute, but the scripts count instances to keep only a single copy of itself running. A few of them count and allow a specific number of instances.
However, a few years ago, all of that stopped working after a cPanel update sometime back in 2013. I didn't know, at the time, what happened but I noticed that there were multiple instances of my script now, running under "jailshell" PIDs. So, I modified my scripts to count those. Didn't work. I changed it to ignore the jailshell copies "grep -v jailshell" but that didn't work fully. My scripts would never run. They would say there were too many instances. So, I increased the counts to compensate and got them running, but over time, multiple copies would spawn far beyond the limit I imposed. Within days, there would be hundreds of instances and the server would grind to a halt.
Tonight, I finally found the solution.
I found this cPanel thread: https://forums.cpanel.net/threads/jailshell-users-not-seeing-processes-in-ps-or-top.351271/
Which sounds like my issue. cPanel says it's not a problem, because they couldn't reproduce it from the command line, but they weren't testing from a shell created by CRON. That is the problem. When CRON runs, it can only see processes launched in ITS shell; not all processes for that user.
So, a bit more digging and I found mention of cPanel making the jailshell also mount /proc with limited capabilities. However, they added an override at one point. It can be found in WHM under "Tweak Settings" -> "System", titled: "Jailed /proc mount method". If you set it to "Always mount full /proc", this will allow CRON launched scripts to see ALL of the processes via ps and top. If you keep jailshell enabled, you still have to ignore them (i.e. grep -v jailshell), but the counts will be accurate again. Hooray!
I tested this fully tonight and my scripts are working again but not spawning out of control.
Note: This does present a security liability in that a shared hosting user (or compromised site on this server) could potentially escape the jail shell using information derived from ps and top. So use with caution. I am willing to take the risk, because I keep my sites updated, write my own code, and cannot use the server for much otherwise. Your situation may warrant more caution, however.

Run php-cgi process

Today earlier my nginx server was 100% CPU usage, the process using all CPU was php-cgi.
I login and kill all php-cgi with this command.
kill -s 9 PID
Now after restarting my server is not working, I see the message "No input file specified.". I google this message, but nothing works, I suppose I have just to start php-cgi again, but can't find how start it.
UPDATE
If I run top command, I can see php-cgi running
1049 root 20 0 336m 20m 10m S 0.0 0.3 0:00.37 httpd
1051 apache 20 0 219m 5472 608 S 0.0 0.1 0:00.55 httpd
1080 root 20 0 20888 1180 592 S 0.0 0.0 0:00.02 crond
1182 root 20 0 19256 976 384 S 0.0 0.0 0:00.00 nginx
1183 nginx 20 0 19856 3176 1364 S 0.0 0.1 0:05.65 nginx
2326 apache 20 0 337m 13m 2512 S 0.0 0.2 0:02.07 httpd
2331 apache 20 0 337m 13m 2564 S 0.0 0.2 0:02.10 httpd
2696 root 20 0 96656 3820 2944 S 0.0 0.1 0:00.18 sshd
2701 root 20 0 12084 1696 1336 S 0.0 0.0 0:00.03 bash
2808 apache 20 0 337m 12m 1988 S 0.0 0.2 0:00.22 httpd
2864 root 20 0 12632 1228 948 R 0.0 0.0 0:00.29 top
2908 ulisses 20 0 183m 11m 6704 S 0.0 0.2 0:00.07 php-cgi
Run ps aux command, also show cgi-php
root 1049 0.0 0.3 344532 20700 ? Ss 14:39 0:00 /usr/sbin/httpd
apache 1051 0.0 0.0 224920 5472 ? S 14:39 0:00 /usr/sbin/httpd
root 1080 0.0 0.0 20888 1180 ? Ss 14:39 0:00 crond
root 1182 0.0 0.0 19256 976 ? Ss 14:43 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1183 0.0 0.0 19856 3176 ? S 14:43 0:05 nginx: worker process
apache 2326 0.0 0.2 345492 13900 ? S 16:56 0:02 /usr/sbin/httpd
apache 2331 0.0 0.2 345480 13944 ? S 16:57 0:02 /usr/sbin/httpd
root 2696 0.0 0.0 96656 3820 ? Ss 17:41 0:00 sshd: root#pts/0
root 2701 0.0 0.0 12084 1696 pts/0 Ss 17:42 0:00 -bash
apache 2808 0.0 0.2 345164 12848 ? S 17:52 0:00 /usr/sbin/httpd
ulisses 2929 0.8 0.1 187732 11976 ? S 18:06 0:00 /usr/bin/php-cgi -c /var/www/vhosts/teclasap.com.br/etc/php.ini
root 2932 0.0 0.0 10480 932 pts/0 R+ 18:06 0:00 ps aux
You can kill all php-cgi by
sudo killall -9 php-cgi

Categories