Home > Back-end >  Blocking a bash script running with &
Blocking a bash script running with &

Time:07-20

I may have inadvertently launched a bash script containing an infinite cycle whose exit condition may be met next century, if ever. The fact is that I launched the script, as I would do with a nohup program, with

bash [scriptname].sh &

so that (as I get it, which is most probably wrong) I can close the terminal and still keep the script running, as was my intention in developing it. The script should run calculation programmes in my absence and let me gather the results after some time.

Now I want to stop it, but nothing seems to do the trick: I killed the programmes the script had launched, I removed the input file the script was getting orders from and - last and most perfect of accomplishments - I accidentally closed the terminal trying to "exit" the script, which was still giving me error messages. How can I check whether the script is running (as it does not appear in "top")? Is the '&' relevant? Should I just ask permission to reboot the pc, if that will work and kill everything?

Thank you.

[I put a "Hi everyone" at the beginning but the editor won't let me show it. Oh, well. It's that kind of day.]

CodePudding user response:

Ok, I'll put it right here to prove my stupidity, as I wandered the internet shortly (after a long wandering before writing this post) and found that the line:

kill -9 $(pgrep -f [SCRIPTNAME].sh)

does the trick from any terminal window. I write this answer to help anyone in the same situation, but feel free to remove the thread if unnecessary (and excuse me for disturbing).

CodePudding user response:

Good you found it, here is another way if you do not use bash -c and run it in current shell not a separate shell.

# put a job in background
sleep 100 &

# save the last PID of background job
MY_PID=$!

# later
kill  $MY_PID
  •  Tags:  
  • bash
  • Related