Loop shell script is being killed when kill child - linux

#! /bin/sh -e
cd /home/pi/projects/3._Raspberry_Client/bin/ARM/Debug
while true;
do
./3._Raspberry_Client.out
sleep 3s
done
I want to start program when it be killed.
So, I make shell script; like sample.
If Run it on bash and do kill -9 something, it perfectly works.
But I want to run this shell script on startup.
I set it in /etc/xdg/lxsession/LXDE-pi/autostart.
like,
./Client_Boot.sh (this is in /home/pi)
then I reboot, shell script works.
But If I kill the program, shell script is also being killed too.
Why this happens? And What should I do?
Thanks for your kindness. And Sry to Bad English.

Related

How to keep a bash script running in the background

I write a simple bash script:
while :
do
sleep 2;
//my code
done
Now I want this bash script always be running.
bash mybash.sh > /dev/null &
When I run above command my bash works fine. but when I close my terminal I think my bash is killed. because it doesn't work as my script make some files when it running.
Run the script "bash script.sh" in terminal and press ctrl+z and then use 'bg' command to put the script in background
#!/bin/bash
while true; do
// your code
sleep 5;
done;
write a bash script and put it that to cron and check once it will start comment the cron it will run in a background.
insted of sleep 5 you can use whatever second you want to put.
For checking your process use below commend to get the details
ps -ef | grep script_file_name
if you find more then one process is running leave one process and rest kill the process for script.
Hope so this will resolve your issue....!!!!

Get the process ID in a Shell script when a process is launched in foreground

In a shell program I want to launch a program and get its PID and save in a temp file. But here I will launch the program in the foreground and will not exit the shell until the process is in running state
ex:
#!/bin/bash
myprogram &
echo "$!" > /tmp/pid
And this works fine i am able to get the pid of the launched process . But if i launch the program in fore ground i want to know how to get the pid
ex :
#!/bin/bash
myprogram /// hear some how i wan to know the PID before going to next line
As I commented above since your command is still running in foreground you cannot enter a new command in the same shell and goto the next line.
However while this command is running and you want to get the process id of this program from a different shell tab/window process then use pgrep like this:
pgrep -f "myprogram"
17113 # this # will be different for you :P
EDIT: Base on your comment or is it possible to launch the program in background and get the process ID and then wait the script till that process gets exited ?
Yes that can be done using wait pid command as follows:
myprogram &
mypid=$!
# do some other stuff and then
wait $mypid
You can't do this since your shell script isn't running -- the command you just launched in the foreground is.

How to kill a shell script and processes it created?

I run a shell script inside php (using shell_exec). I want to kill it and all processes it created when it is in operation. I know the shell script pid. Do you have any suggestion?
I am using ubuntu, php as apache module.
Thank you.
Example:
#!/bin/bash
echo hello
sleep 20
When I kill my script (shell_exec("sudo kill -9 $pid")), the sleep process is not killed which is not desired.
use
pkill -TERM -P pid
will kill the child processes
see this answer
Use this kill command instead:
kill -- -$pid
to kill the running script and all its spawned children.

Bash script on background: how to kill child processes

Well, I'm basically trying to make a bash script runs a node script forever. I made the following bash script:
#!/bin/bash
while true ; do
cd /myscope/
unlink nohup.out
node myscript.js
sleep 6
done & echo $! > pid
I'm expecting that when it runs, it starts up node with the given script, checks if node exits, sleeps for 6 seconds if so and reopen node. Also, I'm expecting it to run in background and writes it's pid (the bash pid) on a file called "pid".
Everything explained above works as expected, apparently, but I'm also expecting that when the pid of the bash script is killed, the node script would stop running, I don't know why that made sense in my mind, but when it comes to practice, it doesn't work. The bash script is killed indeed, but the node script keeps running and that is freaking me out.
I've tested it in the terminal, by not sending the bash script to the background and entering ctrl+c, both scripts gets killed.
I'm obviously miss understanding something on the way the background process works. For god sake, can anybody help me?
There are lots of tools that let you do what you're trying, just two off the top of my head:
https://github.com/nodejitsu/forever - A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
https://github.com/remy/nodemon - Monitor for any changes in your node.js application and automatically restart the server - perfect for development
Maybe the second it's not what you're looking for, but still worth a look.
If you can't or don't want to use those then the problem is that if you kill the parent process the child one is still there, so, you should kill that too:
pkill -TERM -P $PID
where $PID is the parent PID.

Suspending a bash script running in the background

I have a bash script running in the background, and it executes in a loop every 5 seconds. This effects the commands that are being executed at the shell itself.
Is there a way, i can suspend the script execution until the shell is not executing other commands.
thanks
to stop the scipt you can use
kill -s STOP $PID
where $PID contains the process ID of the script. To start it again, you use
kill -s CONT $PID
Renice should help in such scenario.
Using renice you can decrease/increase the priority of the process.
Look man page for more detailed help.

Resources