I need to be able to start a named "session" from within a cron job and run a job within that named session. The job I need to run MAY cause my appliance to not run properly if problem exist and I need to be able to reattach to the named session created if needed/to close it later. Does anyone have any idea how I might do this?


GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:

screen -D -n -s <name> <command>

Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:

screen -D -r <name>

The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).

  • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you? – Marshal Tucker Nov 6 '17 at 19:59
  • 1
    Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.) – DopeGhoti Nov 6 '17 at 20:11
  • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably. – Austin Hemmelgarn Nov 6 '17 at 20:19

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.