2

I work on Linux Mint myself, have remote Ubuntu VPS server with root ssh access. I have script on server which makes long data processing (hours or ever days of processing).

Now I can open console on my machine, connect to VPS via ssh and run script - I see what script is doing in real time in my console (how much data processed, how much left and so on refreshes regulary on my console screen) - this works.

How can I do similar, but without dependencies of my local machine:

  1. Connect to server & run script
  2. Disconnect and left server to do his long processing work, while I can reboot my local machine or ever shut it down without affecting started server script execution.
  3. Have possibility to connect later again and see what is current status of process (same data it showed me in real time) without interrupting process.
DaneSoul
  • 123
  • 5

2 Answers2

6

I think that screen is an ideal solution for you. How it works - you connect to your VPS, type screen, then run your script and detach from the shell (either close your terminal window, or press ctrl+a followed by ctrl+d)

Your script (and shell) will keep running in background. You can view the screen number by typing screen -ls. To restore your session and see the progress/result of your script, get the screen number with screen -ls and then connect to it with screen -r <screen number>. And that's it. After restoring, you can detach in the same manner again.

You can install screen on your VPS with apt-get install screen for Debian based distributions and yum install screen for RedHat based distros.

13dimitar
  • 2,508
  • 1
  • 13
  • 15
  • 2
    Also tmux (http://man7.org/linux/man-pages/man1/tmux.1.html) works almost the same (but it's newer and active developing), choose any you like more http://superuser.com/questions/236158/tmux-vs-screen – toshyak Jan 23 '17 at 14:05
2

You could also run the script in the background and access the output when you need it. My test script will be rather simple:

root@jump:~# cat test.sh
#!/bin/bash
for ((i=0; i<=99999; i+=1)); do
    echo $i
    sleep 1
done

It just prints a number a second. To run it in the background, ill use at:

root@jump:~# at now
warning: commands will be executed using /bin/sh
at> /root/test.sh
at> <EOT>

Note: The EOT is done with Control&D. After that, let the script run, disconnect from the server. When you want to see the output, connect and find the PID of the process:

root@jump:~# ps aux | grep test
root      6312  0.0  2.1  18048  2836 ?        SN   13:08   0:00    /bin/bash /root/test.sh

And get the output. Replace 6312 with the PID of your process:

root@jump:~# tail -f /proc/6312/fd/1
137
138
139
140
141
^C

fd/1 = stdout, fd/2 = stderr

embedded
  • 466
  • 2
  • 6
  • 19
  • 1
    while most likely working it might not be a good idea to use custom scripts for a solution you can easily use tmux and screen for, depending on the setup however you might be unable to install them so this is a valid "workaround" approach in my opinion. – Dennis Nolte Feb 01 '17 at 13:38
  • 1
    Custom scripts? This solution does not use a script. It just does a tail. That's all. No need for terminal multiplexers in any way when basic unix commands can do the job. – embedded Feb 02 '17 at 18:29
  • 1
    you are right, my comment is partly wrong. – Dennis Nolte Feb 03 '17 at 08:49