1

I have a dev and QA web server running on the same EC2. I've created a shell script (which is called via Cron) to check to see if a process is running and if not, restart it.

Shell script:

#!/bin/bash

process="php"
makerun="/usr/bin/php /var/www/html/api/artisan queue:work --queue=high,default --memory=512"


if ps ax | grep -v grep | grep $process > /dev/null
then
  exit
else
  $makerun &>/dev/null &
fi

exit

This all works well and great but it's now time to start the worker for our QA server (the above is dev) and obviously it's not going to do anything since the process name will be the same in both cases.

That said, does anybody know of a way to determine what invoked the process? If I knew that one was launched from the dev path then I could rewrite the bash script to be much smarter and in turn monitor the process for both dev and QA.

Any insight would be appreciated!

  • You run both dev and QA on the same EC2 instance? If so, I'm assuming dev and QA processes run with different arguments? – guzzijason Sep 14 '18 at 22:02
  • 2
    You could look for clues in /proc/. The traditional way to handle this is a pidfile: make the process store its process id to a file and check for a process with that id. – reinierpost Sep 14 '18 at 22:47
  • 1
    Could pass a different environment variable/value for production vs dev. – danblack Sep 15 '18 at 4:50
  • 1
    I am not sure if I understand your problem correctly: On my Linux systems, ps ax (or ps -Alf) already show the complete path of the executable belonging which has been used to start the respective process (at least in many cases). Why can't you use that? – Binarus Sep 15 '18 at 20:02
  • 1
    I have updated my answer (added how you actually can get the last part of each line). – Binarus Sep 17 '18 at 19:05
1

I am not sure if I have understood your problem correctly:

On my Linux systems, ps ax (or ps -Alf) already shows the complete path of the executable which has been used to start the respective process (at least in many cases). Could you eventually use that?

For example, on a box running Debian Stretch (excerpt):

root@charon:~# ps -Alf
4 S message+   447     1  0  80   0 - 11283 SyS_ep Aug31 ?        00:00:00 /usr/bin/dbus-daemon --system --address=syste
4 S root       468     1  0  80   0 - 11625 SyS_ep Aug31 ?        00:00:03 /lib/systemd/systemd-logind
4 S root       674     1  0  80   0 -  3634 core_s Aug31 tty1     00:00:00 /sbin/agetty --noclear tty1 linux
4 S root       708     1  0  80   0 - 17486 core_s Aug31 ?        00:00:00 /usr/sbin/sshd -D
5 S ntp        712     1  0  80   0 - 24467 core_s Aug31 ?        00:00:36 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 109

Hence, to get the complete path, you would have to grep the last part of each line, for example:

root@charon:~# ps -Alf|awk '{print $15}'
/usr/bin/dbus-daemon
/lib/systemd/systemd-logind
/sbin/agetty
/usr/sbin/sshd
/usr/sbin/ntpd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.