Frage

I'm to write a script which will grab all the text from a file (/tmp/pidfile.txt) which is just a pid number, and then store that as a variable (say pidvar) or something and execute the following:

kill -2 pidvar 

Seems simple enough I just don't know how to grab the pid from the .txt file. I have python installed if this helps. Trying to make it easier to kill uWSGI, any suggestions on an alternative would be welcome.

Thanks in advance for any help.

War es hilfreich?

Lösung

The literal answer to your question (using a bash extension to be slightly more efficient) would be

kill -2 "$(</tmp/pidfile.txt)"

...or, to be compatible with POSIX sh but slightly less efficient...

kill -2 "$(cat /tmp/pidfile.txt)"

...but don't do it either of those ways.


pidfiles are prone to race conditions, whereas process-tree-based supervision systems can guarantee that they only ever deliver a signal to the correct process.

runit, daemontools, Upstart, systemd, and many other alternatives are available which will guarantee that there's no risk of sending a signal to the wrong process based on stale data. CentOS is probably the last major operating system that doesn't ship with one of these (though future versions will almost certainly use systemd), but they're available as third-party packages -- and if you want your system to be reliable (detecting unexpected failures and restarting services as soon as they go down, for instance, without having to do it with your own code), you should be using one of them.

For instance, with systemd:

systemctl kill -s SIGINT uwsgi.service

...or, with runit:

sv interrupt uwsgi

...whereas with upstart, you can configure a completely arbitrary restart command to be triggered on initctl reload uwsgi.


For general best-practices documentation on using shell scripts for process management, see the appropriate page on the wooledge.org wiki, maintained by irc.freenode.org's #bash channel.

Andere Tipps

It is generally easier to ask uwsgi to kill itself. You can do this by using the "master-fifo" option in your config, and then send a "q" to the fifo. This is described here: http://uwsgi-docs.readthedocs.org/en/latest/MasterFIFO.html.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top