Master Linux Automation: Startup Scripts, at, and crontab Explained
This guide walks you through essential Linux automation techniques—including boot‑time service startup with chkconfig and rc.d, one‑off scheduling using at, and recurring jobs with crontab and shell scripts—so you can manage web servers efficiently without manual intervention.
When a Linux‑based website is in production, routine maintenance such as resource monitoring, log rotation, and data cleanup often needs to run automatically; this article introduces common Linux automation tasks.
Save manpower – a single script can replace manual work.
Run at night to avoid peak traffic.
Increase accuracy when properly configured.
Reduce hassle by eliminating repetitive command entry.
Boot Startup
Automatically executing commands at boot is a frequent requirement for starting services and processes.
chkconfig command
chkconfig allows you to enable or disable services at specific runlevels.
Runlevel 0: shutdown
Runlevel 1: single‑user mode
Runlevel 2: multi‑user mode without NFS
Runlevel 3: multi‑user mode with NFS
Runlevel 4: unavailable
Runlevel 5: multi‑user mode with graphical interface
Runlevel 6: reboot
Typical chkconfig usage:
<code>chkconfig --list // list current startup services</code><code>xxxd 0:off 1:off 2:on ... 6:off // example output</code><code>chkconfig --add xxxd // add a service</code><code>chkconfig --level 2,3,4 xxxd on // enable at levels 2‑4</code><code>chkconfig --del xxxd // remove a service</code>Editing rc.d files
You can directly edit files under
/etc/rc.d/(e.g.,
rc.local) to create custom boot‑time actions, such as starting Apache with
/usr/local/apache/bin/apachectl start.
Using at for One‑Time Tasks
atschedules a single execution of a command.
<code>#at time // specify execution time</code><code>at> operation // enter the command</code><code>at> Ctrl+D // finish editing</code>Common time formats:
<code>at 14:30 tomorrow // next day at 14:30</code><code>at now + 10 minutes // 10 minutes from now</code><code>at midnight // at midnight</code><code>at 9:00 pm // today at 21:00</code>Check pending jobs in
/var/spool/at. Note that the
atddaemon is disabled by default and must be started manually.
Using crontab for Recurring Tasks
The built‑in
crondaemon runs scheduled jobs defined in crontab files.
Cron Introduction
Cron consists of a daemon and configuration files. User‑specific crontabs reside in
/var/spool/cron/, while system‑wide schedules are in
/etc/crontaband
/etc/cron.d/. Additional directories such as
/etc/cron.hourly,
/etc/cron.daily,
/etc/cron.weekly, and
/etc/cron.monthlyallow scripts to run at those intervals.
Using crontab
Common commands:
<code>crontab -e // edit current user's crontab</code><code>crontab -l // list crontab entries</code><code>crontab -r // remove crontab</code>A crontab line consists of five time fields followed by the command. Operators:
*– every possible value
/– step values (e.g., every 5 minutes)
-– range (e.g., 1‑5)
,– list of specific values
Examples:
<code>0 0 25 12 * // December 25 at 00:00</code><code>*/5 * * * * // every 5 minutes</code><code>* 4-6 * * * // at 4,5,6 AM daily</code><code>* * * * 2,5 // every Tuesday and Friday</code>Combining with Simple Shell Scripts
For complex logic, place commands in a shell script and invoke it from crontab.
<code>#!/bin/sh</code><code>a="hello world"</code><code>echo $a</code>Add the script to crontab, e.g.:
*/5 * * * * /usr/sh/test.sh– runs every five minutes.
Alternatively, execute a PHP script with:
/phppath/php /filepath/test.phpEfficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.