Running shell command in background using (&) sign To run a command or a script to the background, terminate it with an ampersand sign (&) at the end as shown. NOTE: Ending the command with the ampersand sign does not detach the command from you.
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. if you find more then one process is running leave one process and rest kill the process for script.
A script can be run in the background by adding a "&" to the end of the script. You should really decide what you want to do with any output from the script. It makes sense to either throw it away, or catch it in a logfile. If you capture it in a log file, you can keep an eye on it by tailing the log file.
Leave off the quotes
$cmd &
$othercmd &
eg:
nicholas@nick-win7 /tmp
$ cat test
#!/bin/bash
cmd="ls -la"
$cmd &
nicholas@nick-win7 /tmp
$ ./test
nicholas@nick-win7 /tmp
$ total 6
drwxrwxrwt+ 1 nicholas root 0 2010-09-10 20:44 .
drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 ..
-rwxrwxrwx 1 nicholas None 35 2010-09-10 20:44 test
-rwxr-xr-x 1 nicholas None 41 2010-09-10 20:43 test~
Building off of ngoozeff
's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:
cmd="google-chrome";
"${cmd}" &>/dev/null & disown;
&>/dev/null
sets the command’s stdout
and stderr
to /dev/null
instead of inheriting them from the parent process. &
makes the shell run the command in the background. disown
removes the “current” job, last one stopped or put in the background, from under the shell’s job control.In some shells you can also use &!
instead of & disown
; they both have the same effect. Bash doesn’t support &!
, though.
Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}"
rather than "${cmd}"
:
cmd="google-chrome";
eval "${cmd}" &>/dev/null & disown;
If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.
Here's a function for it:
#!/bin/bash
# Run a command in the background.
_evalBg() {
eval "$@" &>/dev/null & disown;
}
cmd="google-chrome";
_evalBg "${cmd}";
Also, see: Running bash commands in the background properly
This works because the it's a static variable. You could do something much cooler like this:
filename="filename"
extension="txt"
for i in {1..20}; do
eval "filename${i}=${filename}${i}.${extension}"
touch filename${i}
echo "this rox" > filename${i}
done
This code will create 20 files and dynamically set 20 variables. Of course you could use an array, but I'm just showing you the feature :). Note that you can use the variables $filename1, $filename2, $filename3... because they were created with evaluate command. In this case I'm just creating files, but you could use to create dynamically arguments to the commands, and then execute in background.
For example you have a start program named run.sh to start it working at background do the following command line. ./run.sh &>/dev/null &
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With