Cron

Questions and mods regarding system management may go here
Forum rules
We've moved! Head over to Synology Community (community.synology.com) to meet up with our team and other Synology enthusiasts!
tigris
Trainee
Trainee
Posts: 13
Joined: Fri Aug 24, 2012 7:22 am

Cron

Unread post by tigris » Mon Aug 27, 2012 12:12 pm

Hi all,

I have a new DS411j with DSM 4.0-2228 installed. I added a cron entry into /etc/crontab which seems to be the stock standard syno crontab. However, I just noticed on a reboot that all entries I put in there get wiped. So that's pretty useless.

I had perviously tried creating my own cron entry on /var/spool/cron/crontabs/<username> but stuff in there just doesn't get executed. So I am at a bit of a loss as to where to put custom cron entries.

Any ideas?

User avatar
Goner
Seasoned
Seasoned
Posts: 590
Joined: Tue Mar 06, 2012 2:27 pm
Location: Rotterdam, Netherlands

Re: Cron

Unread post by Goner » Mon Aug 27, 2012 1:12 pm

Did you use tabs (and not spaces!) when editing /etc/crontab ??

There are 2 crontabs ; etc/crontab & etc.default/crontab. If etc/crontab is not formatted correctly, it gets reset to the default values it seems.

NAS : DS212j with 2 ST2000DL003 in SHR / DSM 5.2-5644 Update 5
LAN : Fritz!Box 7170, 5 Devolo 200/500Mbps homeplugs, 2 5-port switches, Maxxter ACT-WNP-RP-002
HW : Raspberry Pi 2B & 3B, Conceptronic CHD3NET, ACRyan Playon!HD, Eminent EM7075dts, Wii, Wii U, PS2, D-Link DCS-930L
OS : Linux Mint 16 Cinnamon

tigris
Trainee
Trainee
Posts: 13
Joined: Fri Aug 24, 2012 7:22 am

Re: Cron

Unread post by tigris » Mon Aug 27, 2012 1:38 pm

I did use spaces. That's what I use in my Linux crontabs all the time. I changed them to tabs just now and it didn't make a difference.

My /etc.defaults/crontab file is empty, apart from the header line.

User avatar
maxxfi
Compiler
Compiler
Posts: 6794
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: Cron

Unread post by maxxfi » Mon Aug 27, 2012 4:37 pm

I was doing a few quick tests and it seems that after reboot it wipes any cron entry that:
1) is not commented out
2) does not have root as user

Maybe then you can try to set the entry as root but then have the command as "su - <youruser> -c <yourcommand>"
(although it sounds ridiculous to me)
No longer using Synology NAS, moved to more open source solutions.
DS-106j > DS-210j > DS-411

tigris
Trainee
Trainee
Posts: 13
Joined: Fri Aug 24, 2012 7:22 am

Re: Cron

Unread post by tigris » Mon Aug 27, 2012 4:49 pm

It was having "spaces" in the /etc/crontab file that makes it wipe on reboot. Using spaces is not an issue when left running, it works fine. But on reboot, synology software will find the line and delete it. So putting custom crons into /etc/crontab is fine if you use tabs.

However, it would still be nice to put custom crons somewhere else so even an upgrade won't blow them away. But alas, I can't get any crontab files in /var/spool/cron/crontabs/* to run. I also tried creating an /etc/cron.d folder and putting them in there, no good.

I even tried installing cron from ipkg but couldn't get to run any crons either, putting them in /opt/etc/cron.d/ where it already put it's own cron, but even the /opt/sbin version of cron won't run them. Pretty damn confusing really.

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Wed Aug 29, 2012 12:11 am

So, if we use TABS in the file it will work and not get deleted?

I ask because I'm having trouble right now with executing my cron for FlexGet.

http://forum.synology.com/enu/viewtopic ... 38&t=55702

I think I could get past all this as long as I just use the '/etc/crontab' directory. But, the only problem with this, is that I can not stop and start the cron whenever I want correct? It would be nice to do this since I need top make changes to the config.yml file to update the series information and so forth.

Sorry, hopefully I haven't thread jacked anyone (I'm new to this, and trying to put together a write-up on this process for other noobs.)

tigris
Trainee
Trainee
Posts: 13
Joined: Fri Aug 24, 2012 7:22 am

Re: Cron

Unread post by tigris » Wed Aug 29, 2012 3:29 am

Yes, use /etc/crontab. But you will need to change that symlink code back in the S04crond.sh you changed, as it seems to me that the synocron that it runs does not care about /etc/crontab or any file in /var/spool/cron/crontabs other than the "root" file. Since they are symlinked though, it's just easier to edit /etc/crontab than to try and remember the full path to the place where synocron expects it to be.

Simply use tabs instead of spaces, make sure it only runs as ROOT, and make sure to stop/start S04crond.sh any time you make a change to /etc/crontab.

Your line should be something like:

*/5 * * * * root su - crontab -c /opt/local/bin/flexget --cron

The key here is that syncron is very very picky and any line it can't understand will be deleted. Lines it can't understand that i know about so far include: time periods separated by spaces and commands not set to be run by root

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Thu Aug 30, 2012 5:28 am

tigris wrote:Yes, use /etc/crontab. But you will need to change that symlink code back in the S04crond.sh you changed, as it seems to me that the synocron that it runs does not care about /etc/crontab or any file in /var/spool/cron/crontabs other than the "root" file. Since they are symlinked though, it's just easier to edit /etc/crontab than to try and remember the full path to the place where synocron expects it to be.

Simply use tabs instead of spaces, make sure it only runs as ROOT, and make sure to stop/start S04crond.sh any time you make a change to /etc/crontab.

Your line should be something like:

*/5 * * * * root su - crontab -c /opt/local/bin/flexget --cron

The key here is that syncron is very very picky and any line it can't understand will be deleted. Lines it can't understand that i know about so far include: time periods separated by spaces and commands not set to be run by root

Dude, first off . . . Thank you for spending the time to explain this to me. So, here is what I have done . .

1.) Edited 'etc/crontab' to show the following . .

Code: Select all

*/5	*	*	*	*	root	su - crontab -c /opt/local/bin/flexget --cron
Where everything is a TAB until you get to 'su - crontab - c' which are just spaces
Then TAB /opt/local/bin/flexget TAB --cron

I didn't edit my S04crond.sh file, as I didn't quite understand your wordage (I don't know much about the linking and so forth. .) If you could help me out with what I should change my S04 file to, I would greatly appreciate it!!!

Here's what I have in my current S04crond.sh file in '/usr/syno/etc/rc.d' ....

Code: Select all

#!/bin/sh
#
# S04crond.sh - startup script for crond
#
# This goes in /usr/syno/etc/rc.d and gets run at boot-time.

CROND=/usr/sbin/crond


case "$1" in

start)
	if [ -x "$CROND" ] ; then
		mkdir -p /var/spool/cron/crontabs/
		rm /var/spool/cron/crontabs/*
		ln -sf /etc/crontab /var/spool/cron/crontabs/root
		ln -sf /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab
		echo "Starting crond..."
		$CROND
	fi
	;;

stop)
	echo "stop crond"
	kill -USR1 `cat /var/run/crond.pid` > /dev/null 2>&1
	;;

*)
	echo "usage: $0 { start | stop }" >&2
	exit 1
	;;

esac
If I run

/opt/local/bin/flexget --now from SSH, everything works as it should, but it only works that once. I think the main problem I am having is this damn CRON setup and making it work. I would really appreciate your help in figuring this out.

tigris
Trainee
Trainee
Posts: 13
Joined: Fri Aug 24, 2012 7:22 am

Re: Cron

Unread post by tigris » Fri Aug 31, 2012 3:44 am

globalrebel wrote:1.) Edited 'etc/crontab' to show the following . .

Code: Select all

*/5	*	*	*	*	root	su - crontab -c /opt/local/bin/flexget --cron
Where everything is a TAB until you get to 'su - crontab - c' which are just spaces
Then TAB /opt/local/bin/flexget TAB --cron
That part after "su - crontab -c" shouldn't be a tab either. The entire "su - crontab -c /opt/local/bin/flexget --cron" part is the command to be run, so spaces in there are fine. The last tab should be what separates the user who is running the command (in this case, "root") and the command itself (in this case "su - crontab -c /opt/local/bin/flexget --cron").
globalrebel wrote: I didn't edit my S04crond.sh file, as I didn't quite understand your wordage (I don't know much about the linking and so forth. .) If you could help me out with what I should change my S04 file to, I would greatly appreciate it!!!
You've made a previous edit to that file to add "ln -sf /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab". You should remove that, although it's not causing any issues, it's not required. I would also remove the /volume1/homes/crontab/crontab file itself, just to avoid confusion in the future.

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Fri Aug 31, 2012 6:23 am

You've made a previous edit to that file to add "ln -sf /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab". You should remove that, although it's not causing any issues, it's not required. I would also remove the /volume1/homes/crontab/crontab file itself, just to avoid confusion in the future.[/quote]

Dude seriously, thank you soo much. I will make these changes right away. I'll let you know what happens. I'll let you know what ends up happening, and again. . . THANK YOU for helping someone new to Lunix, *unix out!!

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Fri Aug 31, 2012 8:07 pm

tigris wrote: You've made a previous edit to that file to add "ln -sf /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab". You should remove that, although it's not causing any issues, it's not required. I would also remove the /volume1/homes/crontab/crontab file itself, just to avoid confusion in the future.

I didn't get to try this out yet (I had something come up last night) but I will be trying tonight for sure. However, I was wondering about one part of the code in the S04crond.sh file . . .

Code: Select all

stop)
   echo "stop crond"
   kill -USR1 `cat /var/run/crond.pid` > /dev/null 2>&1
   ;;
I see the 'kill -USR1' reference, but I have no 'usr1' account. Will that be a problem?

User avatar
maxxfi
Compiler
Compiler
Posts: 6794
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: Cron

Unread post by maxxfi » Fri Aug 31, 2012 9:07 pm

globalrebel wrote: I see the 'kill -USR1' reference, but I have no 'usr1' account. Will that be a problem?
USR1 is the type of 'signal' that kill command has to send to cron. It has nothing to do with accounts.
No longer using Synology NAS, moved to more open source solutions.
DS-106j > DS-210j > DS-411

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Tue Sep 04, 2012 12:24 am

maxxfi wrote:
globalrebel wrote: I see the 'kill -USR1' reference, but I have no 'usr1' account. Will that be a problem?
USR1 is the type of 'signal' that kill command has to send to cron. It has nothing to do with accounts.
Ok, so I updated everything and it still isn't running on its own.

My S04crond.sh file is now . . .

Code: Select all

#!/bin/sh
#
# S04crond.sh - startup script for crond
#
# This goes in /usr/syno/etc/rc.d and gets run at boot-time.

CROND=/usr/sbin/crond


case "$1" in

start)
   if [ -x "$CROND" ] ; then
      mkdir -p /var/spool/cron/crontabs/
      rm /var/spool/cron/crontabs/*
      ln -sf /etc/crontab /var/spool/cron/crontabs/root
      echo "Starting crond..."
      $CROND
   fi
   ;;

stop)
   echo "stop crond"
   kill -USR1 `cat /var/run/crond.pid` > /dev/null 2>&1
   ;;

*)
   echo "usage: $0 { start | stop }" >&2
   exit 1
   ;;

esac
So, it should now be the way you said it should be. I have also gone to each file in /root/.flexget and set the permissions to 7777 and made the owner root. (I use WinSCP to do this. I select the file, goto properties, and then set the permission to 7777, and make sure the owner is root.)

My crontab file in /etc/ is

Code: Select all

#minute	hour	mday	month	wday	who	command
*/5	*	*	*	*	root	su - crontab -c /opt/local/bin/flexget --cron
All tabs until 'su - crontrab -c /opt/local/bin/flexget --cron' which are all spaces.

I have tried starting the cron using

'/usr/syno/etc.defaults/rc.d/S04crond.sh start' through SSH while logged in as root, it says it is starting crond, but I don't see the flexget process in my resouce monitor. I wait 10 minutes and never see it. (should happen every 5 minutes)

But, if I run flexget through ssh using

Code: Select all

/opt/local/bin/flexget --test -v
The program start and goes through it standard process with no problem. (I even see flexget process in resource monitor)

I don't know what I'm doing wrong. Can anyone please shed some light on this for me?

Also, I know that I am trying to run this as root, but FlexGet stated that it should not be run from root, but it is possible.

http://flexget.com/wiki/InstallWizard/S ... Scheduling

User avatar
maxxfi
Compiler
Compiler
Posts: 6794
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: Cron

Unread post by maxxfi » Tue Sep 04, 2012 9:45 am

I had a shot at running a user's crontab, and I noticed (looking at the source of crond) that it can work (in DSM 4.0 at least) as explained in the thread http://forum.synology.com/enu/viewtopic ... 39&t=13473 but key point is that the user file must be owned by root (as the 3rd post in that thread is recommending). so my suggestion is:
- start from the stock Synology configuration for cron scripts / crontabs
- create a user crontab from UI (or whatever flexget requires)
- telnet/ssh to your DS as root, and create a file /volume1/homes/crontab/crontab and put a test line like this:

Code: Select all

* * * * * crontab date >>/tmp/crontest.out
(where the * are separated by tabs)

- activate the cron entry with:

Code: Select all

ln  -s /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab
- relaunch cron daemon:

Code: Select all

/usr/syno/etc/rc.d/S04crond.sh stop
/usr/syno/etc/rc.d/S04crond.sh start
- wait a minute (use 'date' command) and go check if the file /tmp/crontest.out is created and if so read it. It should contain a timestamp.
- if it works, at least you know that your crontab's user crontab works. Now replace the test crontab entry with the flexget one.
- if the flextget entry doesn't work, try to end the line with a "2>/tmp/flexget.err" to collect error messages and monitor if that file gets created.
No longer using Synology NAS, moved to more open source solutions.
DS-106j > DS-210j > DS-411

globalrebel
Novice
Novice
Posts: 47
Joined: Mon May 14, 2012 9:54 pm

Re: Cron

Unread post by globalrebel » Tue Sep 04, 2012 5:40 pm

Thanks for your help Maxfi.

Just a few quick Q's . .

When you say to start with a stock synology configuration for cron scripts / crontabs. . . . what exactly do you mean? Is there a standard or default template I can/should use? Where can I find this?

I created the user 'crontab' in the UI. I then used WinSCP to login to the folder and create the 'crontab' file. In the file I put . .

Code: Select all

*TAB*TAB*TAB*TAB*TABcrontabTABdate >>/tmp/crontest.out
TAB = where I used a TAB, and then a space was used everywhere else.

Now, when you say Activate the cron entry with :

Code: Select all

ln  -s /volume1/homes/crontab/crontab /var/spool/cron/crontabs/crontab
Where exactly do I put that? Do I put that is the S04crond.sh file? Or do I just type that in on the command line while SSH-ing into the diskstation as root?

I will then re-launch the cron but doing the stop and then start command.

You then say to "wait a minute (use 'date' command)"... Do you mean I type in 'date' in the SSH terminal? Or am I just waiting a minute to allow the cron to run to create the file with the timestamp in it?

Sorry for the questions, just want to be sure I'm doing it correctly.

Locked

Return to “System Managment Mods”