JamPakt
Posts: 4
Joined: Fri May 04, 2018 11:22 pm

Can’t close script that is running in background

Wed May 16, 2018 11:19 pm

Ok, so I created a file called test.sh which essentially closes the oldest running test.sh script, and then opens a webcam interface. (I also made a version that should be run first so it doesn’t close itself.) the problem is that unless I execute the script in the terminal, I can’t use pkill or killall to stop the script. I don’t want the terminal to be visible, so is there some way I can execute it without terminal, and then stop it afterwards? (This should be done with a command that can be implemented into the bash script, not a key combination)

Thanks.

n67
Posts: 697
Joined: Mon Oct 30, 2017 4:55 pm

Re: Can’t close script that is running in background

Thu May 17, 2018 12:27 am

Please post code.

No way to know what's going on without seeing the code.
"L'enfer, c'est les autres"

JamPakt
Posts: 4
Joined: Fri May 04, 2018 11:22 pm

Re: Can’t close script that is running in background

Thu May 17, 2018 1:06 am

The problem isn’t necessarily the code, but I essentially did this within the test.sh code:
sudo pkill -o test.sh
sudo mplayer tv:///dev/video0

This uses pkill to destroy the oldest running test.sh script (which I have another version of without the pkill command), then it opens mplayer to view my webcam.
The problem, is that unless test.sh is open in the terminal, I can’t stop the script with pkill and then rerun it. (Or in other words, the second time the script is run, it closes itself, I’m assuming because it thinks that it’s the oldest running script)

tpyo kingg
Posts: 118
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Can’t close script that is running in background

Thu May 17, 2018 3:04 am

You can check if there is more than one instance and skip right to mplayer if there is only one running.

Code: Select all

newest=$(pgrep -n -x test.sh);
oldest=$(pgrep -o -x test.sh);

echo $oldest, $newest

if test $oldest -ne $newest; then
        sudo pkill -o -x test.sh;
fi;

sudo mplayer tv:///dev/video0;



n67
Posts: 697
Joined: Mon Oct 30, 2017 4:55 pm

Re: Can’t close script that is running in background

Thu May 17, 2018 6:00 am

What's with all the "sudo"s???

You seem to be of the type that assumes that "sudo" is the universal prefix for all commands.

Using sudo here is both:

1) Unnecessary and therefore wrong on theoretical grounds (you should only use root privs when actually needed - and even then, you should think twice before doing so)

2) The cause of the problem. Because when stuff is running as root (via sudo), you won't be able to find it and/or kill it when running as not-root.

I'm willing to bet that if you remove the sudo's, the problem (whatever it is) will become solve-able.
"L'enfer, c'est les autres"

User avatar
PeterO
Posts: 4182
Joined: Sun Jul 22, 2012 4:14 pm

Re: Can’t close script that is running in background

Thu May 17, 2018 7:28 am

The problem is nothing to do with sudo, more that the script kills itself...
It needs some logic so it doesn't try to kill itself. There may be more elegant ways but I think the code below does what you want.

Code: Select all

#!/bin/bash

echo "starting"

myPID=$$
echo "myPID=$myPID" 
echo -n  "allPIDS="
for a in $( ps --no-headers -o pid -C test.sh) ; do echo -n  $a ;
if [ "$a" -ne "$myPID" ] ; then echo -n "<-KILLING IT " ; kill $a ; fi 
done

echo
echo "sleeping"

sleep 10

echo "finished"
PeterO


PS: tpyo kingg's answer is neater :-)
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Troubleshooting”

Who is online

Users browsing this forum: Baidu [Spider], barus, smartroad and 43 guests