2

My ubuntu VM does not run cron jobs with a TTY (not even when I log in and run the command from the user's env).

Because of this, cron can't run tmux or screen, preventing programs (specifically rtorrent in this case) to be run in the background in a simple and attachable way.

What is the best approach to running a program like rtorrent on boot while making it easy to attach, detach, and kill the process from any terminal?

I assume just running the equivalent of rtorrent & should do it, but this is not as convenient as screen/tmux.

7

If you use the -d option with tmux new, it won't attach to or require a tty. From man tmux:

new-session [-AdDEP] [-c start-directory] [-F format] [-n window-name]
            [-s session-name] [-t group-name] [-x width] [-y height]
            [shell-command]
            (alias: new)

Create a new session with name session-name.

The new session is attached to the current terminal unless -d is given. [...]

For example:

tmux new -d -s rtorrent rtorrent

That creates a new tmux session called "rtorrent" and runs rtorrent inside it.

You will probably need to configure ~/.tmux.conf, and run a script to start rtorrent (rather just the bare rtorrent command) in order to setup the run environment correctly.

The user who owns the cron job can attach to the session at any time with:

tmux attach -t rtorrent

The equivalent for Screen is

screen -d -m -S rtorrent rtorrent

from the cron job and screen -S rtorrent -rd to attach later.

|improve this answer|||||
  • Thank you. Is rc.local better than init.d? i.e. should the rtorrent/tmux session be a service? I suppose it depends on whether or not init.d services have access to tty – tbg Aug 5 '17 at 3:13
  • you could write an init.d script too if you want. a systemd unit file is another alternative. either would make it easier to stop/start the rtorrent-in-tmux service on demand. – cas Aug 5 '17 at 3:18
  • BTW, The -d option to tmux new is what prevents it from attaching to (and thus requiring) a tty. From man tmux (search for "new-session"): The new session is attached to the current terminal unless -d is given.. There's no reason why this couldn't be run from cron - but IMO cron is the wrong way to start something like rtorrent so i gave an answer that provided an alternative (imo better) method. – cas Aug 5 '17 at 3:26
  • Thank you for the hint about detached mode! I have it working now. I tried using rc.local but there were some difficulties: the -u option is not recognized for the su command, additionally, it simply wasn't working and I didn't have time to debug. cron it shall be for the time being. Thanks. I marked your answer as correct, but you may want to add the -d hint to the top of your answer for others. – tbg Aug 5 '17 at 3:57
  • i'm too used to the sudo command, which does have a -u option. never mind, i got rid of the rc.local and su stuff. not necessary to answer the question. – cas Aug 5 '17 at 4:18

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.