Crontab issues

hi

I wonder if someone could provide a little guidance on how to use cron under OSMC: I feel like i am very close.

What I’m trying to so.
schedule the shut down of bittorrent sync (btstnc) between 3pm and 11pm. (NB: I’m still using older version of btsync because later versions consume lots of CPU, even when not transferring data. Later versions have a built in scheduler

This is my thinking.
I’ll create a cron job linked to 2 scripts. One to stop btsync service at 3pm, and another to start it at 10pm.

This is what i’ve done so far.

  1. System>MyOSMC>AppStore>install Cron Task Scheduler (Kodi 16.1 Aug 28 2016)
  2. Written a script to stop btsync and put in /usr/bin - I’ve tested script by running it as sudo from command line and it works!
  3. Edited crontab (sudo crontab -e) and put in the line referencing the script. I’ve used sudo crontab -e as i believe this means the script runs as root. My script does not reference sudo, command is just btsync service stop after the usual script bin bash stuff. (as mentioned above i’ve tested and script works).

My crontab entry is 'm h * * * /usr/bin/btsync-stop with the ‘m’ and ‘h’ being the minutes (58) and the ‘h’ being the hour (17). I’ve created the entry using ‘sudo crontab -e’, rather than just under osmc ‘crontab -e’ as i believe (maybe incorrectly that this means script runs as sudo rather than just user.

Feels like i’m really close because my script works, but i can’t schedule it using cron. Feels like I’m missing something obvious. Have i mucked up by putting the script on the /usr/bin directory? Do i need to also install / start something else. Do i need to run the cron app as a daemon?

When i check if something like cron is running it does appear to be.
login as: osmc
osmc@192.168.1.101’s password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Oct 4 17:12:14 2016 from loungehtpc.local
osmc@osmc:~$ service cron status

  • cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service; enabled)
    Active: active (running) since Tue 2016-10-04 14:22:11 NZDT; 3h 59min ago
    Docs: man:cron(8)
    Main PID: 284 (cron)
    CGroup: /system.slice/cron.service
    `-284 /usr/sbin/cron -f
    osmc@osmc:~$

Cheers, Geoff.

can you give the output of;
ls -l /usr/bin/btsync-stop

Maybe even provide the contents of the script.

I believe if you are going to call it like that then the script itself needs to be executable. I think you might also be having user rights issues though. I ten to put my stuff in the system crontab (/etc/crontab) rather than individual user crontabs but that’s because I’m lazy, you could put it in the root user crontab (so that it runs as root by;

sudo su - (to switch to root)
crontab -e (to edit root users crontab)

Edit
A quick google search seems to indicate that your strategy or sudo crontab -e should be fine.

Thanks

Output is:

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Oct 4 18:20:04 2016 from loungehtpc.local
osmc@osmc:~$ ls -l /usr/bin/btsync-stop
-rwxr-xr-x 1 root root 32 Oct 4 17:36 /usr/bin/btsync-stop
osmc@osmc:~$

Pasted Script below.

osmc@osmc:~$ cd /usr/bin
osmc@osmc:/usr/bin$ nano btsync-stop
GNU nano 2.2.6 File: btsync-stop

#!/bin/bash
service btsync stop

Notes. The only other things I can think is that I’m missing a blank line at the end of my crontab thing (various internet opinions on this) or that I don’t have the file extension of .sh on my script, however my script does in fact work, so unlikely to be the .sh thing.

How do my permissions look? I not very adept at interoperating the xwrexewx thing.

Thanks again.

It all looks ok to me. The only other think I can thing of is to adjust the script ot call service with it’s absolute path.

So change;
service btsync stop

to

/usr/sbin/service btsync stop

1 Like

The script needs chmod +x

It looks like it’s already got execute on it already;

osmc@osmc:~$ ls -l /usr/bin/btsync-stop
-rwxr-xr-x 1 root root 32 Oct 4 17:36 /usr/bin/btsync-stop

thanks both for your assistance. I’ve tried your suggestions (and some you haven’t!) however I still can’t use crontab to run script. mmm.

Here is a summary which might help, but pasting all you might be able to spot something I haven’t.

here we go, key elements highlighted. cheers, Geoff.

Script
root@osmc:/usr/bin# nano btsync-stop.sh
File: btsync-stop.sh

#!/bin/bash
service btsync stop

Crontab
sudo crontab -e
(as you can see for testing purposes the cronjob runs * * * * * for every minute)
File: /tmp/crontab.c0jb2d/crontab

* * * * * /usr/bin/btsync-stop.sh

Cron Service Status (tried a few commands as below in found online, note the output of sudo cron -l)

root@osmc:/usr/bin# sudo cron -l
cron: can’t lock /var/run/crond.pid, otherpid may be 263: Resource temporarily u navailable
root@osmc:/usr/bin# sudo cron status
cron: can’t lock /var/run/crond.pid, otherpid may be 263: Resource temporarily u navailable
root@osmc:/usr/bin# service cron status

  • cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service; enabled)
    Active: active (running) since Thu 2016-10-06 15:56:04 NZDT; 34min ago
    Docs: man:cron(8)
    Main PID: 263 (cron)
    CGroup: /system.slice/cron.service
    `-263 /usr/sbin/cron -f

Oct 06 16:28:01 osmc CRON[1907]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 16:28:01 osmc CRON[1906]: (CRON) info (No MTA installed, discarding …t)
Oct 06 16:29:01 osmc CRON[1932]: pam_unix(cron:session): session opened for…0)
Oct 06 16:29:01 osmc CRON[1933]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 16:29:01 osmc CRON[1932]: (CRON) info (No MTA installed, discarding …t)
Oct 06 16:29:02 osmc CRON[1932]: pam_unix(cron:session): session closed for…ot
Oct 06 16:30:01 osmc CRON[1953]: pam_unix(cron:session): session opened for…0)
Oct 06 16:30:01 osmc CRON[1954]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 16:30:01 osmc CRON[1953]: (CRON) info (No MTA installed, discarding …t)
Oct 06 16:30:01 osmc CRON[1953]: pam_unix(cron:session): session closed for…ot
Hint: Some lines were ellipsized, use -l to show in full.
root@osmc:/usr/bin#

Script Properties
osmc@osmc:/usr/bin$ ls -l btsync-stop.sh
-rwxrwxrwx 1 root root 32 Oct 4 17:36 btsync-stop.sh
osmc@osmc:/usr/bin$

How about checking the logs?
sudo journalctl _COMM=cron

And also try changing the crontab to see whats wrong

* * * * * /usr/bin/btsync-stop.sh > /tmp/btsync.log 2>&1

Did you try changing the script to use absolute paths?

thanks, if you mean /usr/bin/btsync-stop.sh rather than just btsync-stop.sh then yes.

Cheers again, Geoff.

No I mean add the following behind the script!

thanks - seems to reference no MTA installed. mmm. Google reveals MTA is a mail relay, and from what I understand cron likes to mail logs. Don’t think this is the answer.

sudo journalctl _COMM=cron
Oct 06 20:52:01 osmc CRON[9154]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 06 20:52:01 osmc CRON[9155]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 20:52:01 osmc CRON[9154]: (CRON) info (No MTA installed, discarding output)
Oct 06 20:52:01 osmc CRON[9154]: pam_unix(cron:session): session closed for user root
Oct 06 20:53:01 osmc CRON[9212]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 06 20:53:01 osmc CRON[9213]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 20:53:01 osmc CRON[9212]: (CRON) info (No MTA installed, discarding output)
Oct 06 20:53:01 osmc CRON[9212]: pam_unix(cron:session): session closed for user root
Oct 06 20:54:01 osmc CRON[9236]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 06 20:54:01 osmc CRON[9237]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 20:54:01 osmc CRON[9236]: (CRON) info (No MTA installed, discarding output)
Oct 06 20:54:01 osmc CRON[9236]: pam_unix(cron:session): session closed for user root
Oct 06 20:55:01 osmc CRON[9305]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 06 20:55:01 osmc CRON[9306]: (root) CMD (/usr/bin/btsync-stop.sh)
Oct 06 20:55:01 osmc CRON[9305]: (CRON) info (No MTA installed, discarding output)
Oct 06 20:55:01 osmc CRON[9305]: pam_unix(cron:session): session closed for user root

with respect to you suggestion to add > /tmp/btsync.log 2>&1 I don’t think this will change much, as the script just run perfectly when I just execute from command line as sudo, I just can’t get the cronjob to run. Did you mean insert after the cronjob command, which isn’t a script but a pointer to a script…sorry noob.

I was referring to calling the ‘service’ command by it’s absolute path of /usr/sbin/service within the script.

you were right, just didn’t see the ‘s’ in sbin. Good job, fixed, Thanks for assist.