Arduino Forum

Products => Arduino Yún => Topic started by: Roque Campos on Nov 21, 2013, 12:34 pm

Title: Problem programming cron tasks in YUN
Post by: Roque Campos on Nov 21, 2013, 12:34 pm
Dear Friends,

I don't understand what I'm doing wrong. I have two scripts, wich I want to run. One of them at startup, and one of them once per day. I writed the following in scheduled tasks  using the luci console:

Code: [Select]
@reboot root /mnt/sd/rhc/start.sh  2>&1 >> /mnt/sd/rhc/rhc.log
12 35 * * * root /mnt/sd/rhc/maintenance.sh  2>&1 >> /mnt/sd/rhc/rhc.log


It does not work, and the only error messages that I can see are in the system log:

Code: [Select]
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at @reboot
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at root
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at /mnt/sd/rhc/start.sh
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at 2>&1
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at >>
Nov 21 12:37:01 Arduino cron.err crond[1523]: user root: parse error at 37


What I'm doing wrong?

Thanks in advance.
Title: Re: Problem programming cron tasks in YUN
Post by: lucad79 on Nov 21, 2013, 03:16 pm
Hi

probably the cron daemon included in the Linino distribution does not support the @reboot macro...
Morover, the correct cron syntax is

minute hour day month day-of-week

so 12 35 is wrong if you meant to schedule that task at 12:35, the correct entry should be

35 12 * * *
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Nov 21, 2013, 05:06 pm
Yes, you are right, it seems that the reboot macro is not supported.

In addition to the minute - hour error, there was another error: the user is not needed. The right command that works is:

35 12 * * * /mnt/sd/rhc/maintenance.sh  > /mnt/sd/rhc/rhc.log

Thank you very much for your help
Title: Re: Problem programming cron tasks in YUN
Post by: lucad79 on Nov 21, 2013, 08:23 pm
you're right, I didn't notice the "root" in front of your command!
Title: Re: Problem programming cron tasks in YUN
Post by: mamu on Nov 22, 2013, 06:40 am
Have you tried to enable cron first?
According to superuser.com one has to for OpenWrt:

By default OpenWrt does not enable the cron service. To start it and enable automatic startup during subsequent reboots, you need to execute the following commands:

/etc/init.d/cron start
/etc/init.d/cron enable
Title: Re: Problem programming cron tasks in YUN
Post by: lucad79 on Nov 22, 2013, 09:37 am
mamu,

the crond used in Linino does not support the tag "@reboot"
bye
Title: Re: Problem programming cron tasks in YUN
Post by: mamu on Nov 22, 2013, 10:18 am
Ok, that is.... Sad
Title: Re: Problem programming cron tasks in YUN
Post by: OttoF on Nov 22, 2013, 12:38 pm
May be the startup initscripts are a possible way to do this
you found it in
LuCi Startup

----------------------------------------------------------------------
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.


wifi-live-or-reset
exit 0
------------------------------------------------------------------------
Title: Re: Problem programming cron tasks in YUN
Post by: mamu on Nov 22, 2013, 09:21 pm
OttoF, had a look right now and it looks promising. I will try this.

BTW: Looks like an official hit spot for doing Linino init stuff. Would be nice to find some sort of documentation if this is the right place.
Title: Re: Problem programming cron tasks in YUN
Post by: federicofissore on Nov 24, 2013, 02:17 pm
I'm not sure it's the correct way to call it, but OpenWRT (as well as many linux distros) is "init" based, meaning it starts services (so called "daemons") with "init scripts"

That gives you two correct ways to do something at boot time. Modify /etc/rc.local adding your scripts (easiest and quickest way) or adding an init script. Init scripts are usefull only if you need something to run BEFORE something else. That's the case for SSL and GPG key regenaration, for example.

I suggest rc.local: it's easy and it's the LAST thing to run when your yun starts up
Title: Re: Problem programming cron tasks in YUN
Post by: mamu on Nov 24, 2013, 02:28 pm
Yep, added the following line to rc.local using LuCI and it works like a charm!
Code: [Select]
(sleep 10;python /www/sd/fg100_yun/server.py)&
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Dec 01, 2013, 11:42 am
Is there an easy way to redirect the cron logs to another file and not use the system log file?

I tried modifing the cron start scritp in  /etc/init.d/cron, replacing the line

ln -s /etc/crontabs /var/spool/cron/ 2>/dev/null

with

ln -s /etc/crontabs /var/spool/cron/ 2>/another/path

But the cron is still logged to the system log file.

Thaks
Title: Re: Problem programming cron tasks in YUN
Post by: mamu on Dec 01, 2013, 01:04 pm
Have you cross-checked with what is displayed in LuCI "System : Scheduled Tasks"?
Title: Re: Problem programming cron tasks in YUN
Post by: lucad79 on Dec 01, 2013, 01:31 pm
2> is actually redirecting only the STDERR (standard error) stream, not the STDOUT.
Are you trying to redirect the output of the commands you put in the crontab or the log of the cron daemon?
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Dec 01, 2013, 02:28 pm
My intention was to redirect the ouput of the daemon to a log script in another location but I can live without it. Instead, I redirected the output of the scripts that are called by the cron. It is more or less the same thing.

But I have a curiosity. I have a cron job every minute. Then, every minute a string like that

Code: [Select]
Dec  1 16:58:01 Arduino cron.info crond[30828]: crond: USER root pid 4605 cmd /mnt/sd/rhc/historical_1m.sh


is wroted in the system log. Maybe I'm confused, but, it is dangerous to abuse of the log scripts in the linino memory? I mean, I have read that it is not recommended to use the internal storage of the Linino because of the limited number of writes...

Can this be a problem?
Title: Re: Problem programming cron tasks in YUN
Post by: NewLine on Dec 01, 2013, 06:32 pm
I have always tried to avoid writing any logs, lock files, etc... on the 'default' location which is typically somewhere in the Linino's memory. I tried to put all those files on the SD-card, just for the reason you mentioned. It would be nice to get confirmation that this indeed the best thing to do to avoid writes on the Linino internal memory.. (note that I am not (yet) using cron)
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Dec 02, 2013, 12:13 am
I agree with you and I think that this can be a problem. But per default the from output is logged to syslog and I don't, know how change it.

Maybe the solution is to reduce the level of the messages that are logged to the syslog, I don't, know how to do this also.
Title: Re: Problem programming cron tasks in YUN
Post by: federicofissore on Dec 03, 2013, 04:50 pm

It would be nice to get confirmation that this indeed the best thing to do to avoid writes on the Linino internal memory.. (note that I am not (yet) using cron)


The less you use the flash, the longer the yun will last. Flash memory has a limited number of writes (I don't know the exact number though: some say 10.000) just as your sd card BUT
1) if the flash of the yun wears out, the yun is gone
2) sd cards are so much bigger and therefore have so many blocks that it's hard to write 10.000 times on each of them
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Dec 03, 2013, 05:23 pm
Yes, I understand you very well. But what I want to explain is that the string that are logged to files like syslog, are logged by default. I mean, it is not my decision: I program a cronjob every minute, and every minute the syslog is writed.

Maybe this behaviour may be disabled by default... or at least, I need to know how to change it.

Hope you understand my not very good English. :~
Title: Re: Problem programming cron tasks in YUN
Post by: NewLine on Dec 04, 2013, 11:55 am
I think this is an important issue with regards to the lifetime of a Yun.
If the default location where e.g. cron drops its log-file can not be changed, an alternative might be to lock directories for writing?
Title: Re: Problem programming cron tasks in YUN
Post by: Roque Campos on Dec 04, 2013, 12:29 pm
I was searching through the internet, and I found the following page:

http://wiki.openwrt.org/doc/howto/log.essentials

and the following paragraph:

Quote
Output
Syslogd writes the log messages it receives into a file or into the RAM ringbuffer (option -C). The file is a file, it can be accessed with cat, less, vi, etc. The data in the RAM ringbuffer should be accessed with logread. You can of course use pipes, like logread -f | nc 192.168.1.1 514 or logread -f » /mnt/share/logfile (reasonable on non-flash media, see usb.storage or nfs.client) or pretty much whatever you want.


Then I did a little test with my arduino, turning it off and on, and it is true, all the messages of the syslog are gone. I suppose that this means that the syslog file is not written to the flash memory. It seems that the syslog file is writtent to the ram ringbuffer.

In this case, I understand that there is not a problem having cron jobs wich outputs data to the syslog, because the flash memory is not used.

Am I right?

Thanks in advance.
Title: Re: Problem programming cron tasks in YUN
Post by: OttoF on Dec 04, 2013, 03:40 pm
i hope so, because i have a cronjob every minute  :smiley-eek-blue:
Title: Re: Problem programming cron tasks in YUN
Post by: lucad79 on Dec 04, 2013, 11:13 pm
yes don't worry... to be sure run

Code: [Select]
ps | grep syslogd

and you'll find it runs with the -C option...