Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help with killing bash background process

Featured Replies

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 &

 

 

 

 

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.

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.

  • Author

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.

  • Author

Thanks again WeeboTech.  Using the .pid files has really worked out well for me.

Archived

This topic is now archived and is closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.