2

I would like to download and run a script in the background so the task is independent of the shell and its exit. Moreover this script should be run as sudo, using:

echo MY_PWD | sudo -u MY_USER -S ...

So it just needs a single line of code in my SSH-Session, which does the authentication and creates the background task including multiple lines. Furthermore the setup-script should be removed after the successful or unsuccessful execution of the NEW_SCRIPT_NAME.sh The lines of code are:

wget https://URL_TO_SCRIPT.sh -O NEW_SCRIPT_NAME.sh
sed -i 's/\\r$//' NEW_SCRIPT_NAME.sh
chmod +x NEW_SCRIPT_NAME.sh
sudo nohup NEW_SCRIPT_NAME.sh &
rm NEW_SCRIPT_NAME.sh

I tried to this:

echo raspberry | sudo -u pi -S wget https://URL_TO_SCRIPT.sh -O NEW_SCRIPT_NAME.sh && sed -i 's/\\r$//' NEW_SCRIPT_NAME.sh && chmod +x NEW_SCRIPT_NAME.sh && nohup ./NEW_SCRIPT_NAME.sh & && rm NEW_SCRIPT_NAME.sh

This causes the following error:

-bash: syntax error near unexpected token `&&'

Which can be fixed by removing the & in the nohup line, but this will cause the script to be executed in foreground not in background, which is necessary for my purposes.

  • 8
    instead of hard-coding a password into script or leaving it visible in the command history, consider using NO_PASSWORD directive in the sudoers file. – MelBurslan Jun 17 '16 at 15:12
  • This will be a setup script, which should install and setup the basics, so I'll don't want to edit the sudoers file – user3191334 Jun 17 '16 at 15:53
  • 1
    You have an awful lot of && directives on your line. I am not sure what flavor of Linux is running on your r-pi, but some shells may not be able to handle so many cascading conditions. I suggest running with one && condition first then adding another one and so on. This way you will see where it is failing. Which may help you diagnose why it is failing too. On anther note, editing sudoers file is neither hard nor a taboo. So, consider using it. This way you can eliminate one pipe out of your long one-liner command. – MelBurslan Jun 17 '16 at 16:21
  • @user3191334 If you run the script in a separate process, the following rm could run too soon, deleting the script before it finishes. – Stefan Schmiedl Jun 17 '16 at 16:26
3

This is wrong syntax in bash:

nohup ./NEW_SCRIPT_NAME.sh & && rm NEW_SCRIPT_NAME.sh

From Shellcheck:

Line 1:
nohup ./NEW_SCRIPT_NAME.sh & && rm NEW_SCRIPT_NAME.sh
                             ^-- SC1070: Parsing stopped here. Mismatched keywords or invalid parentheses?

You can not simply run something & && something, because putting the first command into the background prevents you from waiting for the script return code.

You can either run whole pipeline under nohup in in background (preferably as a script):

nohup ./run.sh &

or if you really want to have everything in one command, you need to wait for the result:

nohup ./NEW_SCRIPT_NAME.sh & wait && rm NEW_SCRIPT_NAME.sh

which will wait for the script to finish, before it will remove it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.