0

I created a shell script named df.sh:

#!/bin/sh
while true ; do 
df -hT
sleep 30
done &

then I run ./df.sh (I give execute permission).

After running it, I want to terminate the loop but I can't.

2
  • what is the exact thing that you want to send to background ? the hole while ? you want to log it ? – Wissam Roujoulah Jan 23 '17 at 9:15
  • The whole while – Saumik Satapathy Jan 24 '17 at 14:00
2

Use

$ pkill -f df.sh

That will send a termination signal to the script and the script will terminate as soon as the current sleep 30 call has finished.

Alternatively, use ps to find the process ID of the script and send a termination signal to it that way:

$ ps

$ kill pid

(where pid is the integer process ID of the script as listed by ps)

0

First of all if that is you hole script i recommend you to use & when you calling the script not at the end of the while:

./df.sh &

I recommend you to use kill to send a terminate signal or kill -9 to send a kill signal if the process doesn't responding after getting the pid of the process using ps with grep or pgrep, you can use pkill, but, careful it will call all df.sh processes if you where running more than once, and if you use it with pkill -f it will kill any process that match df.sh pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.