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).
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 thekill $WBtrick, as every single command is executed in the foreground, so a simpleexitwill suffice. Summary: ssh into your VPS -> executetmux-> type the commands you listed (without theWB=$$; (, the) &at the end and substitutingkill $WBwith a simpleexit) -> disconnect, detach and re-attach as neededIf you can't or don't want to use tmux, put your commands in a file. The problem here is basically that
nohupcan'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.shfile:#!/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 $WBThen you can execute it with
nohup /opt/myscript.sh $$ &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-cargument.As your script starts with a 2 hour sleep, you may want to consider the
atprogram. You can executeat now + 2 hoursand 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 withatqthat your command sequence has been scheduled for execution.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
-cargument, 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