Sunday, April 3, 2016

Enabling cron, the scheduling service on 600 / 700 / 1100 / 1200R



If you're familiar with UNIX skip down to step 6 for the commands to add to userScript (note the capital "S"). Also see note about crontab command and writing your cron jobs by hand.

In this post I'll show you what is required to configure cron so that jobs can be scheduled. This allows you to create custom jobs to run at a certain time/day. For whatever reason this services isn't enabled by default, but this is easy to address and should be upgrade safe. Meaning if you upgrade the firmware this setting should be saved. This also means we'll need to create/edit the userScript file. This is a file read during boot up. Each line will be executed as the devices boots up. For Gaia users this is like a custom rc.local file.

So, let's start off. This is a quick list of what we're going to do.

  1. Create the symbolic link in the startup file so crond can be found. 
  2. Create the directory for crontabs. These are scripts that tell cron when and what to run. 
  3. Start cron (to make sure it will run correctly). 
  4. Create/Edit userScript file. 
  5. Reboot (to make sure cron starts on boot up). 
  6. Create a test cronjob!  (edit userScript again and reboot again).

So let's begin!

Step 1. Here we are going to create a symbolic link (think of this as a shortcut) to busybox for cron.

[Expert@FW]# ln -s /bin/busybox /bin/crond
The link should look something like this now.
[Expert@FW]# ls -l /bin/crond
lrwxrwxrwx 1 root root 12 Apr 3 19:08 /bin/crond -> /bin/busybox
[Expert@FW]#

Step 2. Create the crontabs directory for crond. This is where your scheduled jobs will need to be placed.

[Expert@FW]# mkdir -p /var/spool/cron/crontabs/
Directory should look like this.
[Expert@FW]# ls -ld /var/spool/cron/crontabs/
drwxr-xr-x 2 root root 40 Apr 3 19:12 /var/spool/cron/crontabs/
[Expert@FW]#

Step 3. Start cron!

[Expert@FW]# /bin/crond
once running you should see something like this for output.
[Expert@FW]# ps aux | egrep '[c]rond'
root 1822 0.0 0.1 4748 656 ? Ss 16:38 0:00 /bin/crond
[Expert@FW]#

Step 4. Lets do this on boot up!
Add each start up command to /pfrm2.0/etc/userScript. This file isn't created by default. This is what it should look like.

[Expert@FW]# ls -l /pfrm2.0/etc/userScript
-rw-r--r-- 1 root root 76 Apr 3 19:24 /pfrm2.0/etc/userScript
[Expert@FW]# cat /pfrm2.0/etc/userScript
ln -s /bin/busybox /bin/crond
mkdir -p /var/spool/cron/crontabs/
/bin/crond
[Expert@FW]#

Step 5. Reboot!

Once the system is done rebooting you should see something like this run expert mode.
[Expert@FW]# ps aux | egrep '[c]rond'
root 1822 0.0 0.1 4748 656 ? Ss 16:38 0:00 /bin/crond
[Expert@FW]#


Step 6. Setup a test cronjob!

I've created a file called /var/spool/cron/crontabs/root. This is the file that cron jobs will be run from. In this example I created a job that will simply create a message in the /var/log/messages file every min.

[Expert@FW]# ls -l /var/spool/cron/crontabs/root
-rw------- 1 root root 44 Apr 3 19:34 /var/spool/cron/crontabs/root
[Expert@FW]# cat /var/spool/cron/crontabs/root
* * * * * echo "testing123testing" | logger
[Expert@FW]# egrep 'testing' /var/log/messages
2016 Apr 3 19:37:01 FW cron.notice crond[1822]: USER root pid 1957 cmd echo "testing123testing" | logger
2016 Apr 3 19:37:01 FW user.notice root: testing123testing
[Expert@FW]#

Everything looks good, now create you own crontab via an echo statement in /pfrm2.0/etc/userScript. This way your cronjob will be created at boot up. You'll need to be careful about only using " " inside your command if you need quoting as part of your cronjob. Be sure to escape $ and ! otherwise odd things may happen. Here is our final /pfrm2.0/etc/userScript. Note I used append statements ( >> ) so that if you add more than one job the last line won't clobber the previous.

[Expert@FW]# cat /pfrm2.0/etc/userScript
ln -s /bin/busybox /bin/crond
mkdir -p /var/spool/cron/crontabs/
/bin/crond
echo '* * * * * echo "testing123testing" | logger' >> /var/spool/cron/crontabs/root
chmod 600 /var/spool/cron/crontabs/root
[Expert@FW]#


Next up, we'll use cron and userScript to setup a job to automate backups! Hopefully this wasn't too long winded.

NOTE: Do not use the command crontab. It will create your crontab, however it won't be saved as /var is on / which is a rootfs. This means no data is saved on a reboot.

2 comments:

  1. Nice..! Thank you John for your contribution, very good chichi :-)

    ReplyDelete
  2. That is really cool. Who knew you didn't have to install any 3rd party apps to run cron! Busybox is so cool.

    ReplyDelete

Danger Will Robinson!