0

I'm trying to crfeate a bash script that - amongst other things starts sunwait:

/root/bin/sunwait sun up -0:00:00 54.453158N 11.013071E; mycommand &

Sunwait is a binary that will wait for astronomical events to happen before executing a given command. I used the ampersand at the end to send it to the background so my script can continue to do other things but this does not work and i don't know why.

Executing the command directly on the shell will also not send sunwait to the background. I tried nohup too with no success.

I'm on Ubuntu 14.04 Server LTS. Why is it not working?

2 Answers 2

1

Sunwait is a binary that will wait for astronomical events to happen before executing a given command.

After examining your syntax I guess that this interpretation is wrong. In your example sunwait executes nothing, in fact there is no command given to it. I am not familiar with sunwait, but quick research revealed to me that it just waits for astronomical event and then finishes.

In your case it goes like this:

  • The shell reads the line, encounters semicolon ; and executes everything to that point -- that is the sunwait with its command line options.
  • The shell waits for sunwait to finish.
  • Eventually sunwait finishes, so the shell proceeds to mycommand and executes it in background because of ampersand &.

So, direct answer to your question is most common: it is not "working" (not doing what you want) because it is working (doing what you command).

Fix:

  1. Either { /root/bin/sunwait sun up -0:00:00 54.453158N 11.013071E; mycommand; } &

  2. or /root/bin/sunwait sun up -0:00:00 54.453158N 11.013071E && mycommand &

In the first line the two commands inside {} appear to & as if it was single command, so everything goes to the background. Simple and often good enough. The second case is slightly different: mycommand will run if and only if sunwait finishes without error (unless sunwait is poorly coded). It is the behavior you expected in the first place, isn't it?. Personally I find the second fix more elegant. Imagine you made a mistake typing mycommand and realized after a while that it is a ticking bomb. Killing sunwait will trigger the bomb in 1st scenario, defuse it in 2nd.

1
  • thanks for the detailed explanation and for discussing the possible fixes!! I feel kind of stupid now and && is working like a charm :)
    – Niksac
    Mar 31, 2015 at 22:21
1

Your line has two commands, separated by a semicolon (;). You need to either send both commands into the background:

/root/bin/sunwait sun up -0:00:00 54.453158N 11.013071E & mycommand &

Or, if you want the commands to run one after the other, run them both in a subshell and background that:

( /root/bin/sunwait sun up -0:00:00 54.453158N 11.013071E; mycommand ) &

I believe the latter example is what you're trying to achieve as the first one will start both commands right now and background them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .