I'd like to run several commands on the command line.
In a normal case this is simple:
#cd /home && ls && echo "OK"
root web support
OK
However when one of the commands ends itself on a &
this doesn't seem to work:
#killall vsftpd && /usr/sbin/vsftpd & && echo "OK"
-sh: syntax error: unexpected "&&"
OK
I've tried without the single trailing &
but this obviously halts the processing of the latter echo
. Just for fun tried a triple &
but this also returns an error.
So my question; how can I get
killall vsftpd
/usr/sbin/vsftpd &
echo "OK"
executed on one single line?
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
.
You can encapsulate commands (or sequences of commands) in parentheses, like so:
# killall vsftpd && (/usr/sbin/vsftpd &) && echo "OK"
However, this doesn't make much sense semantically as the &&
token means “run subsequent command if previous command successful” (i.e., return value of 0), and the single &
puts the command preceding it into the background (continuing immediately with the next command), which always yields success.
In your case, there really isn't any way to determine the success of running the vsftpd
command when it is backgrounded, unless the executable offers command-line arguments running the thing as a daemon so you needn't background it manually.
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