I want to start a process in a terminal using cron. I want the process to start in a terminal, so that I can continuously see the output of the process on the terminal, and kill it / restart it etc. I know I can do this via screen, by using "screen -p -X stuff ", but I've lately run into weird issues with screen freezing (Screen session freezes, output stops, but process continues running. Reclaiming screen possible?), and was wondering if there was a way to start a process via the cron in a terminal without using screen? I can create the terminal beforehand and rename it etc. by hand, in case that helps.

  • You can use something like * * * * * echo test >/dev/pts/2 where 2 is the terminal you have active. Use w to see details. – Valentin Bajrami Jul 2 '15 at 18:00
  • Thanks! This is nice, but the problem with this is that in case I accidentally kill the terminal, in order to start the process again, I'll have to change the cron. If there was a way this could work with terminal names, then that would be perfect. That way if I accidentally exit "term2", I can start a new terminal and simply rename it "term2". – user10 Jul 2 '15 at 18:02

Here is a solution which will both unlockpt() a new pty descriptor and write its ptsname() to stdout for you.

<<\C cc -xc - -o pts
#include <stdio.h>
int main(int argc, char *argv[]) {
        if(unlockpt(0)) return 2;
        char *ptsname(int fd);
        return argc - 1;

Which just compiles a tiny little C program that attempts to call unlockpt() on its stdin and if successful prints the name of the newly created and unlocked pty to stdout or else silently returns 2.

On a linux system - given appropriate permissions (in general, these are most easily achieved by adding oneself to the tty group) - a new pty descriptor might be obtained as easily as...

exec 3<>/dev/ptmx

...to get the master-side fd in the current shell then, provided you have compiled the program above in ./...

slave=$(./pts <&3)

...will both make your new descriptor practically usable and put its device name in the value of the shell variable $slave.

Next, provided the util_linux package has been installed on your system, you can install a new setsid() process there like:

setsid -c sh 3>&- <>"$slave" >&0 2>&1

Which will launch an interactive sh process as the session leader of your pty and quit, essentially backgrounding the interactive shell.The backgrounded shell will interpret anything written to >&3 as user input. A particularly useful thing to write might look like:

echo "exec >$(tty) 2>&1" |
cat   - /dev/tty    >&3

To redirect all output from your background shell and its children to your current terminal temporarily, while simultaneously redirecting all of your keyboard input back to your background shell for so long as cat survives. The backgrounded shell's /dev/tty is the pty in slave, though, and so the staus-quo is restored as easily as:

echo 'exec >/dev/tty 2>&1' >&3

The same kind of thing can work from cron, but you would want just script th i/o instead, of course, or to setup some pipe relay to do the juggling for you automatically.

See How do I come by this pty and what can I do with it? for more.

  • I was hoping there would be a simpler way, but this should work. Thanks for the detailed answer. – user10 Jul 17 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.