Reputation: 55
If I try to execute this code in bash shell will work fine:
date && echo "hello world"
But if I want to run date in background and then use echo it does not work:
date & && echo "hello world"
Edit:
The final task is run a script that connect to vpn:
# openvpn /etc/openvpn/client/my_vpn.ovpn
And if this work that appear a message. The problem is that vpn command is always running and the next command not show. Ej:
# openvpn /etc/openvpn/client/my_vpn.ovpn && notify-send "VPN connect"
I tried:
# openvpn /etc/openvpn/client/my_vpn.ovpn & && notify-send "VPN connect"
The second message never run, so I tried run a more simple command for test with date.
Upvotes: 0
Views: 784
Reputation: 50750
Because just like semicolon and line break characters, ampersand is a command separator too.
date ; && echo 'hello world'
and
date
&& echo 'hello world'
won't work either.
If you did
{ date & } && echo 'hello word'
however, it'd work. But that doesn't make any sense, asynchronous commands (e.g. date &
) always return zero.
Upvotes: 0
Reputation: 139
Need more information about your task. Because the exact answer depends on the conditions that you did not say.
For example:
If your task is to run a code in the background and, if it succeeds, print a message to standard output, then this is done like this:
{ date && echo "hello world"; } &
Or
If you have a task, run something in the background and later get the result, then this is done like this:
coproc dateDoneUndone {
date && {echo "Done"; } || {echo "UnDone"; }
}
# some code
readarray -u ${dateDoneUndone[0]} ans;
echo "Result: ${ans[@]}"
In order to better understand what this code does, you need to read documentation about redirections, job control and coprocesses.
Upvotes: 1
Reputation: 241858
If you run a command in the background, what would be the meaning of &&
? You don't wait for the command to finish, so you don't know whether it was successful or not. Just use the &
operator alone to separate the commands:
date & echo 'hello world'
Upvotes: 1