I want to execute a set of commands 2 hours ahead from current time (i.e, in a timeout).
I need to keep using Bash regularly while these commands are timed-out (i.e, the commands need to run in background, as with
&
).Finally, I need the commands' stdout printed to the terminal of the session which is highest in all session hierarchy.
Why I want to do this:
I need this as part of a script that installs PHPmyadmin (PMA), and then deletes it after 2 hours, for security reasons. This solution was recommended to me by several people of the Information security field (if it comes alongside filtering of port 3306, and some would add HTTPS as well).
What seemed as solution at start:
I can timeout commands with either sleep
or at
utilities, see:
cat << EOF
sudo nohup sleep 2h
echo "hello"
echo "welcome"
EOF
Or:
cat << EOF | sudo at 'now + 2 hours'
echo "hello"
echo "welcome"
EOF
The problem with these methods (as they are):
The problem is that the commands do not run in background and their stdout won't be printed to the terminal of the session highest in hierarchy.
Notes:
- I say "highest session" or "session highest in hierarchy" or "1st session" just to emphasis that the original session of the commands might be lost because of unintentional or intentional closing of the session window (or other similar reasons like a sudden reboot or a 2 minute power outage) and in this case I'll probably start another session, which will be 1st in session hierarchy, before the commands' timeout of 2 hours.
at
on the VPS, and then I turn on the PC and opens a new session and login to the VPS from it --- This is for me the highest in hierarchy even I opened several few more sessions while this first one is open. – Dec 29 '16 at 14:06