Keeping rtorrent running with cron

randac56

Member
May 25, 2018
915
0
16
It has been a frequently asked question on how to start rtorrent at boot or how to make an init script.
The answer is to simply use the user's crontab.
Init scripts are meant for system wide daemons. While you _can_ make init start rtorrent for a user it is not correct practice.

Code:
#!/usr/bin/env bash
USER=${USER:-$1}
PATH=${PATH:-"/usr/local/bin:/usr/bin:/bin:/home/$USER/bin"}

## [USAGE] crontab -e
## Examples:
# Run every minute * * * * * ~/rtorrent_cron <username>
# every 10 minues */10 * * * * ~/rtorrent_cron
# on reboot @reboot ~/rtorrent_cron
# hourly @hourly ~/rtorrent_cron

COMMAND="dtach -n /home/$USER/rtorrent.dtach rtorrent"
#COMMAND="tmux new -s rtorrent -d rtorrent"
#COMMAND="screen -dmS rtorrent rtorrent"

## Uncomment if you want to log these actions
#LOG=/home/$USER/rtorrent_cron.log
#MSG="$(date "+[%d %b %H:%M:%S]") CRON: Starting rtorrent..."

start_rtorrent() { #test if rtorrent is running, if not, start it
if [[ -z $(pgrep -u $USER rtorrent) ]]; then
$($COMMAND) && sleep 1
[[ $LOG && ! -z $(pgrep -u $USER rtorrent) ]] &&
echo "$MSG ok!" >>$LOG || echo "$MSG FAILED" >>$LOG
fi
}
start_rtorrent >/dev/null
exit 0

To grab this script in one command run
Code:
wget 'http://pastebin.com/raw.php\?i\=CB4ba35C' -O ~/rtorrent_cron && chmod +x ~/rtorrent_cron

you may need to pass a <username> arg at the end of the script for ancient cron daemons
uncomment the COMMAND variable you use on your system, dtach/tmux/screen
uncomment the LOG and MSG variables if you want to see some action logged
Read the USAGE and your golden.
 

shwetha17

Member
May 24, 2018
785
0
16
hey won, sky, also having an issue with getting this to work on 12.04 server

- running the script from the shell works fine: dtach and rtorrent are started
- running via cron @reboot is not working. No log generated, no dtach and rtorrent processes run, but syslog shows: CRON[3239]: (subsy) CMD (/usr/scripts/rtorrent_cron)

any ideas?
 

randac56

Member
May 25, 2018
915
0
16
you might need to put the username on the command with ubuntu's cron daemon.
Code:
@reboot /usr/scripts/rtorrent_cron subvertbeatsdid you try this?
 

shwetha17

Member
May 24, 2018
785
0
16
Thanks for the reply sky

Pretty sure I tried that last night per the [USAGE] but will give it another go and report back later.
 

shwetha17

Member
May 24, 2018
785
0
16
No luck I'm afraid. Tried with and without the username parameter being specified, and moving the script to a couple of different locations

Per syslog the cron job is being run:

Quote
CRON[3260]: (subsy) CMD (~/rtorrent_cron subsy)​


Enabled the log and it shows (expected per the script):

Quote
[19 Dec 13:13:34] CRON: Starting rtorrent... FAILED​


Again, running the script from the shell works just fine
 

lisas4567

Member
May 25, 2018
773
0
16
cron's environment is not that of a login shell, so differences in that and resulting problems are common.
 

shwetha17

Member
May 24, 2018
785
0
16
right, some cron daemons are dumber than others. ubuntu's probably has no idea what ~ is, so making everything an absolute path in the script. replace all the variables with what they should be.
 

peshua19

Member
May 25, 2018
897
0
16
Of course it does, in a USER crontab, where a concept like "home" makes sense.

Also, in there, setting HOME= explicitely helps with any problems. The same goes for LANG and PATH, and a MAILTO also never hurts for diagnosing problems.
 

randac56

Member
May 25, 2018
915
0
16
I had previously tried putting the script in several different locations, inside and outside of the user home, and had tried using an explicit path in the script.

No joy.
 

peshua19

Member
May 25, 2018
897
0
16
Update.

Could not get dtach to run in the user crontabs.

However, using screen worked perfectly first time.

So, still no idea as to the cause of the problem, but the workaround is perfectly acceptable for me.