2

I have the following command set which I sometimes run on my VPS. Basically it works fine; It runs in the background and does what I need.

WB=$$; (
       sleep 2h;
       echo "Removing PMA and exiting";
       phpdismod mcrypt mbstring;
       apt-get purge phpmyadmin -y;
       service apache2 restart;
       sed -i 's/Include \/etc\/phpmyadmin\/apache.conf/ /g' /etc/apache2/apache2.conf;
       kill $WB
) &

The only thing I miss:

The only thing I miss in this command set is a nohup. It's important for me to use nohup in case I've closed the session or had a power outage in my end --- With a nohup, I promise the command set will still be scheduled for execution in the VPS, in such cases.

How could I add a nohup to this command set?

  • It's better to use at command for scheduled jobs. – Ipor Sircer Dec 29 '16 at 13:23
  • How would you use this very same command set in at ? Please share it in an answer. – JohnDoea Dec 29 '16 at 13:46
3

You have a few options, all of which preserve your requirement of terminating your foreground session at the end (which you do with kill $WB).

  1. If possible, use tmux (or screen). It's the most obvious solution for your use case: a disconnection on your end will just cause a detachment of the tmux session, thus the commands you issued will not stop; after that, you could even re-attach to that session if you wanted to check its output (instead of tail -f nohup.out). You wouldn't even need the kill $WB trick, as every single command is executed in the foreground, so a simple exit will suffice. Summary: ssh into your VPS -> execute tmux -> type the commands you listed (without the WB=$$; (, the ) & at the end and substituting kill $WB with a simple exit) -> disconnect, detach and re-attach as needed
  2. If you can't or don't want to use tmux, put your commands in a file. The problem here is basically that nohup can't execute grouped commands (the concept of subshell is exclusive to bash, you can't execute one with nohup), so you could put your code e.g. in the /opt/myscript.sh file:

    #!/bin/bash
    
    #You will pass the PID of the foreground shell as its first argument
    WB=$1
    
    sleep 2h;
    echo "Removing PMA and exiting";
    phpdismod mcrypt mbstring;
    apt-get purge phpmyadmin -y;
    service apache2 restart;
    sed -i 's/Include \/etc\/phpmyadmin\/apache.conf/ /g' /etc/apache2  /apache2.conf;
    kill $WB
    

    Then you can execute it with nohup /opt/myscript.sh $$ &

  3. If you can't or don't want to put them in a file, just execute them like this: nohup bash -c " ...your commands here... ; kill $$" &. The $$ here will still be referring to the outer foreground shell, as it is substituted before it is executed as the bash -c argument.

  4. As your script starts with a 2 hour sleep, you may want to consider the at program. You can execute at now + 2 hours and then type the commands you want to execute 2 hours from then (with 1 minute granularity). Once you have typed all the commands you need, you can exit the "at shell" with Ctrl+D and check with atq that your command sequence has been scheduled for execution.

  5. You can also execute your script as a heredoc. It's basically the same as option 3, but instead of specifying your commands as the -c argument, you give them to bash's standard input. Just be aware that if your script expects arguments, you must specify them during the call to bash, like this:

    nohup bash -s -- firstArgument secondArgument <<'EOF' &
    ....paste your code here.....
    EOF
    
  • 1
    If you prefer to have the output of your commands on the tty itself, tmux is the tool you want to use (and your only choice, as neither "nohup" nor "at" prints the output to your tty). Install tmux on your VPS, then try this: ssh into your VPS --> execute "tmux" --> execute "while true; do echo test; sleep 1; done" (you will see "test" printed out each second) --> press Ctrl+B, then press "d" to detach your session --> execute "exit" to disconnect from your VPS --> ssh into it again --> execute "tmux attach" and you will see that, while you were disconnected, the loop kept working. – Mario Vitale Dec 29 '16 at 22:01
  • Any way to auto disabling this message about output redirected to /home/user/nohup.out ? – JohnDoea Dec 29 '16 at 22:10
  • 1
    Yes, if you don't want the "nohup.out" message from showing, you can add "2>/dev/null" at the end of the command from option 3, before the "&". Thus, the complete command would be ---> nohup bash -c " ...your commands here... ; kill $$" 2>/dev/null & – Mario Vitale Dec 29 '16 at 22:10
  • Also, if you have new questions please create new ones. – Mario Vitale Dec 29 '16 at 22:23
  • 1
    Yes, and I deleted comments that might heavy on newcomers. It's good your replies are there as it helps. It now worked. Thank you for everything. A few days I ate straw with this and you helped me in a dark time. – JohnDoea Dec 29 '16 at 22:26

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.