Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly stop bash script?

I have this in /etc/supervisor/conf.d/myscript.conf

[program:my_script]
directory=/var/www/myfolder
command=/var/www/myscript.sh
numprocs=1
process_name=%(program_name)s
autostart=true
autorestart=true
user=root
stopsignal=KILL

This is content of myscript.sh:

#!/bin/bash
cp -n /var/www/binaryfolder/binaryfile /var/www/myfolder
/var/www/myfolder/binaryfile

This is working but when I stop supervisor this binaryfile keeps running.

I know there will be questions like why don't you copy that file manually and then execute by supervisor, but that is not possible because that file is dynamically passed to myscript.sh.

Thank you

like image 965
iWizard Avatar asked Oct 27 '25 05:10

iWizard


1 Answers

Since starting binaryfile is the last command, you can easily solve the problem by modifying myscript.sh as

#!/bin/bash
cp -n /var/www/binaryfolder/binaryfile /var/www/myfolder
exec /var/www/myfolder/binaryfile

Please note that exec is added at the beginning of the last line which replaces the shell process with binaryfile process so supervisord stops that process. For more information you can check

http://veithen.github.io/2014/11/16/sigterm-propagation.html

If you totally want solve this from supervisord configuration and your supervisord version is greater or equal to 3.0b1, you can add stopasgroup=true parameter.

So your new config file should be

[program:my_script]
directory=/var/www/myfolder
command=/var/www/myscript.sh
numprocs=1
process_name=%(program_name)s
autostart=true
autorestart=true
user=root
stopsignal=KILL
stopasgroup=true

For more information about supervisord configuration, please check: http://supervisord.org/configuration.html#program-x-section-settings

like image 193
Gökçer Gökdal Avatar answered Oct 29 '25 07:10

Gökçer Gökdal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!