queeg Posted August 13, 2010 Share Posted August 13, 2010 I have a bash script in a file. It's got a while loop in it and most of the time it's in sleep command. I'd like to be able to find it by name in the process stack and kill it. Is it possible to search for name or text to find it's pid? I'm running it like this: myscript.sh & Link to comment
BRiT Posted August 13, 2010 Share Posted August 13, 2010 Try using "killall myscript.sh" I think base unRAID has killall installed. The other thing you can do is "jobs", which will list what you have running in the background on the current shell. Then you can do a "kill %1" to kill item number one, or "kill %3" to kill the third one. Link to comment
WeeboTech Posted August 14, 2010 Share Posted August 14, 2010 It's common for jobs that are designed to run in the background to put their pid in a file. i.e. In your script add. echo $$ > /var/run/myscript.pid trap "rm -f /var/run/myscript.pid" HUP INT QUIT TERM EXIT Then when you need to kill $(</var/run/myscript.pid) The problem is your sleep command may be a separate process. see example: root@unraidvm:/tmp# ./myscript.sh & [2] 2765 root@unraidvm:/tmp# ps -ef | grep $(</var/run/myscript.pid) root 2765 2716 0 19:51 pts/0 00:00:00 /bin/bash ./myscript.sh root 2766 2765 0 19:51 pts/0 00:00:00 sleep 999 root 2768 2716 0 19:51 pts/0 00:00:00 grep 2765 The problem is the sleep command could keep the shell running root@unraidvm:/tmp# kill -0 $(</var/run/myscript.pid) root@unraidvm:/tmp# echo $? 0 root@unraidvm:/tmp# kill $(</var/run/myscript.pid) root@unraidvm:/tmp# echo $? 0 root@unraidvm:/tmp# kill -0 $(</var/run/myscript.pid) root@unraidvm:/tmp# ls -l /var/run/myscript.pid -rw-r--r-- 1 root root 5 Aug 13 19:51 /var/run/myscript.pid root@unraidvm:/tmp# ps -fp $(</var/run/myscript.pid) UID PID PPID C STIME TTY TIME CMD root 2765 2716 0 19:51 pts/0 00:00:00 /bin/bash ./myscript.sh root@unraidvm:/tmp# try a construct like this. #!/bin/bash echo $$ > /var/run/myscript.pid trap "rm -f /var/run/myscript.pid;exit" HUP INT TERM QUIT EXIT while [ -f /var/run/myscript.pid ] && sleep 3 do date done Then you can do. root@unraidvm:/tmp# ps -fp $(</var/run/myscript.pid) UID PID PPID C STIME TTY TIME CMD root 2902 2716 0 20:04 pts/0 00:00:00 /bin/bash ./myscript.sh and kill $(</var/run/myscript.pid) This assumes you do the proper existence of the pid file So the proper test would be. [ -f /var/run/myscript.pid ] && kill $(</var/run/myscript.pid) [ -f /var/run/myscript.pid ] && ps -fp $(</var/run/myscript.pid) or you can remove the pid file and the job will end after the next cycle. rm -fv /var/run/myscript.pid The above construct does not take into account duplicate processes. It starts to require more and more housekeeping as you increase the requirements. There's all kinds of cool stuff you can do with the read command automatic time out and a fifo but I think that may be a bit more advanced then you want. Yet... the cool concept is, with a fifo you could send commands to the background job to do various tasks within the loop. let me know if you want examples. Link to comment
queeg Posted August 14, 2010 Author Share Posted August 14, 2010 This is in the script file. while [ -f /var/run/myscript.pid ] do /sbin/hdparm -z /dev/sdh || sleep 9 sleep 1 done Having it check for the existence of the file might be the best way. I want to make certain it stops when I want it to. Link to comment
queeg Posted August 14, 2010 Author Share Posted August 14, 2010 Thanks again WeeboTech. Using the .pid files has really worked out well for me. Link to comment
Recommended Posts
Archived
This topic is now archived and is closed to further replies.