1

I want to execute a set of commands 2 hours ahead from current time (i.e, in a timeout).

  1. I need to keep using Bash regularly while these commands are timed-out (i.e, the commands need to run in background, as with &).

  2. 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.
  • So your command has to survive arbitrary numbers of power cycles? – Edd Dec 28 '16 at 16:39
  • 2
    Between this and your other question I've lost confidence that what you are doing is sensible. Have you properly considered simply stop/start the PMA service, or allow/deny access to a specific port? – Edd Dec 28 '16 at 16:42
  • I want it to survive as much session interruptions has possible. I don't want to deny port 80. stopping or starting the service can be nice but will also require a proper timeout as I need. –  Dec 28 '16 at 21:25
  • Please could you expand on what makes a session the "highest"? If you have multiple running sessions, which one should show the message? The write(1) utility may be all you need. – JigglyNaga Dec 29 '16 at 14:00
  • The one I opened at first, is to me the "highest" in hierarchy... For example, if I had a power outage while the commands are still scheduled with 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
  • As I noted on your question on AU: You should set up an MTA and get mails for cron/at job output, instead of hacking your way around. You don't have to actually send to an external server, mail can be delivered locally. I don't understand why you have this fixation with getting output in your current shell session. – muru Dec 29 '16 at 16:05

1 Answers1

0

Linux handles everything in form of files from devices to ttys.

So what I would suggest is sending the output to both a file and a tty like this:

cat << EOF | sudo at 'now + 2 hours' | tee /var/tmp/at_output.log
echo "hello"
echo "welcome"
less /var/tmp/at_output.log
EOF

Now all you need to do is to view the output file either in a screen session, or via text viewer like cat or tail.

For example, just run tail -f /var/log/at_output.log from your current tty and it will show you a live feed of the output starting with the last 10 lines.

You are also able to view it inside a screen when you switch to one, that you'll first need to create:

To create a screen session type: screen -S at_proc

You'll be presented with a screen session which is a portable tty allowing you to connect and disconnect without breaking the command running and keeping the history, here you'll be able to run your command uninterrupted.

To disconnect from the screen session just hit ctrl + a + d.

To reconnect to a session just type screen -r at_proc or if it's the only session then just screen -r.

When you're done with the screen session just retach to it and type exit or hit ctrl + d like you would do on a regular tty.

Yaron
  • 233