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:
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 serviceEditing 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.
#at time // specify execution time</code><code>at> operation // enter the command</code><code>at> Ctrl+D // finish editingCommon time formats:
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:00Check pending jobs in /var/spool/at. Note that the atd daemon is disabled by default and must be started manually.
Using crontab for Recurring Tasks
The built‑in cron daemon 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/crontab and /etc/cron.d/. Additional directories such as /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly allow scripts to run at those intervals.
Using crontab
Common commands:
crontab -e // edit current user's crontab</code><code>crontab -l // list crontab entries</code><code>crontab -r // remove crontabA 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:
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 FridayCombining with Simple Shell Scripts
For complex logic, place commands in a shell script and invoke it from crontab.
#!/bin/sh</code><code>a="hello world"</code><code>echo $aAdd 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.phpSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient 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.
