How to keep a bash script running in the background - linux

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....!!!!

Related

Loop shell script is being killed when kill child

#! /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.

How Can I Run an Infinite Loop from a Bash Script with Output to Foreground

So, I want to run the below command from a bash script and have it output to the shell, however, all my attempts result in the script running in the background:
while [ 1 ]; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap"; sleep 1; done
When I run the command directly in the shell prompt, it outputs as expected. The application, SNGREP, launches with the specified parameters and works well.
I have experimented with sending the command to Screen, but it still ends up in the background. I have also tried modifying the command by sleeping first (as follows):
while sleep 1; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap"; done
It, too, goes to the background but then runs fine if I type it directly into the shell prompt. What else can I try to get the command to output to the foreground when run from a bash script? Any help is appreciated, thanks.
PS. My end goal is to launch SNGREP in a Putty window from a Windows Batch File. I've got everything working, but this last bit.
It is not clear from your command why it is running in background, rather it should run on foreground only. You can try with below, redirect(2>&1) the output to standard output always:-
while [ 1 ]; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap" 2>&1; sleep 1; done

run the script every 30 minutes bash

I want to run the script every 30 minutes with cron but I have problem with my code.
In every 30 min I have to kill old script and run it again. I have somethink like this, but it is not working:
cd /var/www/scripts
pkill -f bot
now="$(date +%Y%m%d%H%M%S)"
screen -S bot
node mybot.js >> logi/logi_$now.txt
You may not use screen for running things in background in a script. Use ampersand (&) to background a process and nohup so it won't be killed when cron script exits. Also remember a subprocess PID in a file.
Something like this:
kill -- "$(cat mybot.pid)"
now="$(date +%Y%m%d%H%M%S)"
nohup node mybot.js >> "logi/logi_$now.txt" &
echo $! > mybot.pid
use crontab :
crontab -l
*/30 * * * * /path/to/your/command
save and run
The line
node mybot.js >> logi/logi_$now.txt
is never reached, as screen -S <session name> will start a screen session and therefore a new shell and connect to it. The rest of the script would only execute once that 'inner' session terminates.
screen is more for interactive use. Calling it in a script like this is rather strange. I guess you want to have node mybot.js >> logi/logi_$now.txt running in the background, so that your script can terminate while node keeps running. See Redirecting stdout & stderr from background process and Node.js as a background service for options how to do that.

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.

Running shell script command after executing an application

I have written a shell script to execute a series of commands. One of the commands in the shell script is to launch an application. However, I do not know how to continue running the shell script after I have launched the application.
For example:
...
cp somedir/somefile .
./application
rm -rf somefile
Once I launched the application with "./application" I am no longer able to continue running the "rm -rf somefile" command, but I really need to remove the file from the directory.
Anyone have any ideas how to compete running the "rm -rf" command after launching the application?
Thanks
As pointed out by others, you can background the application (man bash 'job control', e.g.).
Also, you can use the wait builtin to explicitely await the background jobs later:
./application &
echo doing some more work
wait # wait for background jobs to complete
echo application has finished
You should really read the man pages and bash help for more details, as always:
http://unixhelp.ed.ac.uk/CGI/man-cgi?sh
http://www.gnu.org/s/bash/manual/bash.html#Job-Control-Builtins
Start the application in the background, this way the shell is not going to wait for it to terminate and will execute the consequent commands right after starting the application:
./application &
In the meantime, you can check the background jobs by using the jobs command and wait on them via wait and their ID. For example:
$ sleep 100 &
[1] 2098
$ jobs
[1]+ Running sleep 100 &
$ wait %1
put the started process to background:
./application &
You need to start the command in the background using '&' and maybe even nohup.
nohup ./application > log.out 2>&1

Resources