How do I run multiple background commands in bash in a single line?
Exactly how do you want them to run? If you want them to be started in the background and run sequentially, you would do something like this:
{ sleep 2; sleep 3; } &
If you want sleep 3
to run only if sleep 2
succeeds, then:
sleep 2 && sleep 3 &
If, on the other hand, you would like them to run in parallel in the background, you can instead do this:
sleep 2 & sleep 3 &
And the two techniques could be combined, such as:
{ sleep 2; echo first finished; } & { sleep 3; echo second finished; } &
Bash being bash, there's often a multitude of different techniques to accomplish the same task, although sometimes with subtle differences between them.
Bash executing multiple commands in background in same line
New answer:
as @CharlesDuffy correctly remarked; my old answer created a subshell, which is entirely unnecessary. In fact the &
sign also terminates the line; so no more need to add an extra ;
. Bash is complaining because a single line containing only a ;
is not a valid command. Bash reads your command as:
cd /scratch/
nohup sh xyz.sh>>del.txt &
;
exit
Therefore you should just remove the ;
after your nohup line, and (again; thanks @CharlesDuffy); only call the nohup command if you succeeded to enter the /scratch/
directory; using &&
means the next command is executed only if the first one succeeds.
cd /scratch/ && nohup sh xyz.sh>>del.txt & exit
Old answer:
You can try putting your command between quotes if you are in a bash shell
cd /scratch/ ; `nohup sh xyz.sh>>del.txt &` ; exit
you can take a look at this question
bash: start multiple chained commands in background
I haven't tested this but how about
print `(touch .file1.lock; cp bigfile1 /destination; rm .file1.lock;) &`;
The parentheses mean execute in a subshell but that shouldn't hurt.
Multiple commands on a single line in Linux
First, if you want to run multiple commands in one line, separate them by a ;
:
cmd1 ; cmd2 ; cmd3
The &&
is the logical and operator. If you issue
cmd1 && cmd2
cmd2
will only run if cmd1
succeeded. That's important to mention (also see below).
If you use the &
to run a command in background simply append the next command without the ;
delimiter:
cmd1 & cmd2
The &
is not a logical operator in this case, it tells bash to run cmd1
in background.
In your case, the commandline needs syntactically look like this:
killall vsftpd && /usr/sbin/vsftpd & echo "OK"
However, I guess you really meant this:
killall vsftpd ; /usr/sbin/vsftpd & echo "OK"
because otherwise you would not be able to start the process if it is not already running, since killall would return a non zero return code.
Even having this the code is quite fragile. I suggest to use your operating systems facilities to start vsftp as a daemon. I mean facilities like the command start-stop-daemon
.
PHP how to run multi bash command in background
I think you also need to handle sleep
's stdout/stderr.
( sleep 5 > /dev/null 2>&1; ...; ) &
Or you can put the redirection after ( ... )
:
( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
Run multiple commands, but run the last one in the background
To run one or more commands in a separate process, enclose that series of commands in parentheses. As specified in the Single Unix Specification, §2.9.4 “Compound Commands”:
( compound-list )
Execute compound-list in a subshell environment […]
To group one or more commands in the same shell process, enclose that series of commands in curly braces:
{ compound-list ; }
Execute compound-list in the current process environment. […]
That's true for any POSIX shell (so it also works in Bash).
So your example can be changed to:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
( mongod & )
That may be good because you want the mongod
process separated. On the other hand, a more general answer would be to group the list of commands within the same shell process:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
{ mongod & }
Both these are described in the above documentation references.
Multiple bash commands in background
ffmpeg will automatically suspend if it doesn't have access to standard out.
You have two choices:nohup /usr/bin/ffmpeg ...
Will take care of the redirect for you, and place the output of ffmpeg in a file called nohup.out
Or add 2>&1>/dev/null
after the closing bracket in your argument to exec(). This will send both standard out and standard err to /dev/null.
Related Topics
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
Add a Prefix String to Beginning of Each Line
Nasm Segmentation Fault on Ret in _Start
Cannot Connect to the Docker Daemon At Unix:/Var/Run/Docker.Sock. Is the Docker Daemon Running
Environment Variable Substitution in Sed
./Configure: /Bin/Sh^M: Bad Interpreter
Filter Log File Entries Based on Date Range
Can't Call C Standard Library Function on 64-Bit Linux from Assembly (Yasm) Code
How to Merge Two "Ar" Static Libraries into One
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
Have Bash Script Answer Interactive Prompts
How to Shutdown a Spring Boot Application in a Correct Way
How to Find Certain Character Position in Shell