I have an Perl script that runs all night, through a terminal window (Win10 putty.exe) on my Ubuntu server. As with many home networks, I occasionally lose my internet connection, which kills my connection and the script.
Running another Perl script through cron (which checks if my script is running), I am able to restart the Perl script.
However it is running without a window.
Is there a method that when I putty into Ubuntu, I can open a terminal window that shows the output from my Perl script which is now running?
Thank you.
You can redirect the output from the cron script to a file >> /path/filename.log
Or start the script with screen: screen -Sdm perl /path/to/script.pl
Run the script in
screen
, ie. before you start the script, typescreen
(if you get "command not found", you need first to install thescreen
package). It will just display system prompt as if nothing has changed, but since now you got a virtual terminal that survives loss of your connection. Script will continue to run in that terminal even when you disconnect.When you lose connection, after reconnecting to your server, type
screen -ls
. This will list yourscreen
sessions. The output can look like this:("myserver" and "myuser" will be your actual hostname and username respectively). If you see "Detached" as above, it means that server correctly noticed that connection was lost and the session has been properly disconnected. In that case, you can reattach to that session with the command:
where of course the number 19923 is the PID of the
screen
process that you got from thescreen -ls
command.However, it might be that the server did not notice disconnection and your
screen
session is still "Attached" to the session that no longer exists. In that case you need first disconnect yourscreen
session before re-attaching to it, you can do this by specifying additionally the-d
parameter:That is a commonly used and de facto standard solution for running programs that should be protected from accidental closing of terminal session and continue to run even if that happens.
When your script finishes and returns to OS, then first
exit
command typed at the OS prompt will termninatescreen
and return you back to regular terminal session (you will see a message "screen is terminating"). The secondexit
command will log you out.