5

What happens when a screen/tmux-like solution is needed (to leave a process running for a long time, but not with nohup because interaction could be needed) - but screen/tmux is not available on the given machine, and installing screen/compiling it isn't a solution?

Q: Are there any "short" perl scripts that can be generally (or not generally) used to achieve this goal (on Linux/AIX/etc.)?

only this is needed:

  1. start the perl script
  2. start the given thing that takes long time
  3. de-attach from the terminal, ex.: because working notebook needs to be shut down
  4. re-attach to the console to see/control the started long running process, just like in screen

Does anybody has a perl script like this?

1
  • 3
    It may help if you comment on the answers you've received to explain why they're not satisfactory to you. Commented Jan 13, 2014 at 14:30

3 Answers 3

11

perl may not be the most appropriate unless the IO::Pty module is installed.

First, maybe you don't need anything complicated if the given-thing-that-takes-long-time (gtttlt) can work OK with pipes.

mkfifo input
nohup gtttlt <> input > output 2>&1 &

to start the gtttlt (input from a named pipe, output to a regular file). Then you attach to it with:

tail -f output & cat > input

If pipes won't do and you need a terminal, then you would want to use something that can start a pseudo-terminal like expect, zsh's zpty, perl's IO::Pty or sshd:

mkfifo input
nohup ssh -tt localhost gtttlt <> input > output 2>&1

And to attach:

stty raw -echo; tail -f output & cat > input

Note that it's not like implementing screen or tmux. screen and tmux implement a terminal emulator. When you reattach, you see the content of the screen of that terminal emulator. Here when you reattach, you only see the last 10 lines of output (though you can adjust by passing a -n 123 option to tail). And we're just passing along all the data to/from the host terminal.

2

This is not exactly what you asked for but I use at to do this. I often launch commands on remote servers from my laptop and then want to shutdown the laptop without losing the command's output. at allows me to launch jobs at a given time and I just redirect the job's output and error streams to files.

For example, if I want to run my_script.pl I will create a little file called foo.txt with the following contents:

my_script.pl > out 2>er

and then run it with

$ at now < foo.txt

I can then log off the server, take my laptop home and check the output by sshing to the server and running

$ tail -f er

or

$ tail -f out

You can also give at jobs from its shell (hit CtrlD to exit):

$ at now
warning: commands will be executed using /bin/sh
at> my_script.pl > out 2>er
at> <EOT>
job 10 at Thu Sep  5 00:39:00 2013
0

As an alternative to the "at" suggestion above, I like this one better:

  1. Start your perl script as usual "perl script.pl"
  2. Do the user interaction stuff and get into the part that takes long
  3. press ctrl+z to put it in the background and type "bg". The process runs now in tbe background
  4. run "jobs" to see what number your process got. It is most probably 1.
  5. run "disown %1" to deatch the progam to your current terminal.
  6. you are now safe to close the terminal (type "exit" or just press the close button)

As an addition, you can redirect output to a file or named pipe. Unfortunately I don't think there is a way to attach the process again to your terminal.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .