Managing Embedded Linux |
Save the shell script using any file name. E.g., fixtime
How to run the shell script automatically when the kernel boots up
Copy the example shell script fixtime to directory /etc/init.d, and then use chmod 755 fixtime to change the shell script mode. Next, use vi editor to edit the file /etc/inittab. Add the following line to the bottom of the file:
ntp : 2345 : respawn : /etc/init.d/fixtime
Use the command #init q to
Cron—daemon to Execute Scheduled Commands
This function is only available for firmware version V1.5 (and later versions). Start Cron from the directory /etc/rc.d/rc.local. It will return immediately, so you don’t need to start it with ‘&’ to run the background.
The Cron daemon will search /etc/cron.d/crontab for crontab files, which are named after accounts in /etc/passwd.
Cron wakes up every minute, and checks each command to see if it should be run in the current minute. When executing commands, output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such a user exists).
Modify the file /etc/cron.d/crontab to set up your scheduled applications. Crontab files have the following format:
mm | h | dom | mon | dow | user | command |
month | hour | date | month | week | user | command |
|
|
The following example demonstrates how to use Cron.
How to use cron to update the system time and RTC time every day at 8:00.
STEP1: Write a shell script named fixtime.sh and save it to /home/.
#!/bin/sh
ntpdate time.nist.gov hwclock
STEP2: Change mode of fixtime.sh
#chmod 755 fixtime.sh
STEP3: Modify /etc/cron.d/crontab file to run fixtime.sh at 8:00 every day.
Add the following line to the end of crontab:
* 8 * * *root /home/fixtime.sh
STEP4: Enable the cron daemon manually.
#/etc/init.d/cron start
STEP5: Enable cron when the system boots up.
Add the following line in the file /etc/init.d/rc.local
#/etc/init.d/cron start