Starting with the following code , my objective was to create a restart command for my python bot.
result = {
#cmds here
prfx and 'restart':lambda _: call('./restart.sh', shell=True),
}[cmd](_)
room.message(result)
I have a lot of commands in this dictionary so I summarized the format.
The command calls the shell script (restart.sh) , and is supposed to kill the bot process (bot.py) , and then reference to another shell script that then starts the bot process again.
[restart.sh]
pgrep -f bot.py # pid
pkill -9 -f bot.py # kills the matching pid
sh ./start.sh #run start.sh
exit 0
[start.sh]
python bot.py
When running the restart command , the bot process is ended and does not continue the rest of the script.
[example : Bash]
Connecting to MySQL database...
connection established.
Connection closed.
ONLINE
[chatroom] Bot: ONLINE!: [ip]
[chatroom] user: >restart: [ip]
168747
169448
It will just show the two processes and terminate.
pkillwill kill your parent. Useexecve()to replace the old copy of the bot with the new one in memory directly. You don't need anypkillat all.pgrepif you're being run from the PID you want to replace? You can just look at your parent PID -- much less volatile and risky than string-matching on names.kill -9unless you really, really need to; it prevents processes from doing housekeeping, like flushing their write buffers on exit, so you can lose log messages or have datastores left in an unclean state).