Master Linux Automation: Startup Scripts, at, and Cron Made Simple
This guide explains how to automate common Linux tasks such as boot‑time service startup, one‑off scheduling with at, and recurring jobs with crontab, covering runlevel management, rc.d editing, command syntax, and integrating shell scripts for efficient system operations.
When operating a Linux‑based website, routine maintenance such as monitoring resources, rotating logs, and organizing data often requires automated execution of tasks. This article introduces common Linux automation techniques.
Save manpower – a single script can replace manual commands.
Run tasks at night to avoid peak traffic and keep daytime performance optimal.
Increase accuracy when configurations are correct.
Reduce hassle by eliminating repetitive command entry.
Boot‑Time Startup
Automatically executing commands at system boot is a frequent requirement for starting services and processes without manual intervention.
chkconfig command
The
chkconfigutility configures services to start at specific runlevels.
Linux runlevels:
0 – shutdown
1 – single‑user mode
2 – multi‑user mode without NFS
3 – multi‑user mode with NFS
4 – unused
5 – multi‑user mode with graphical interface
6 – reboot
Typical
chkconfigusage:
<code>chkconfig --list // list current startup services
xxxd 0:off 1:off 2:on ... 6:off // example output showing service enabled at levels 2‑5
chkconfig --add xxxd // add a service to the startup list
chkconfig --level 1,2,3 xxxd on // enable service at specified levels (default 2‑5)
chkconfig --del xxxd // remove a service from the startup list</code>Editing rc.d files
Directly editing files under
/etc/rc.d/(including
rc?.d,
rc,
rc.sysinit,
init.d) allows custom boot‑time actions. For example, editing
/etc/rc.localwith
vimcan add a line such as
/usr/local/apache/bin/apachectl startto launch Apache on boot.
Using at for One‑Time Scheduled Tasks
The
atcommand schedules a single execution of a command at a specified time.
<code># at time // schedule a job at the given time
at>operation // type the command to run
at>Ctrl+D // finish editing</code>Common time specifications:
<code>at H:m tomorrow // next day at hour H minute m
at now + n minutes/hours/days/weeks // after n time units
at midnight // at midnight
at H:m pm/am // today at specified time</code>Scheduled jobs are stored 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, together with
crontab, handles periodic job scheduling.
Cron Overview
cronis a small subsystem consisting of a daemon and configuration files present on almost all UNIX‑like systems. The daemon can be identified with
ps aux|grep cron.
crontab Locations
/var/spool/cron/– per‑user crontab files, named after the user.
/etc/crontab– system‑wide scheduled tasks.
/etc/cron.d/– additional crontab files or scripts.
/etc/cron.hourly,
/etc/cron.daily,
/etc/cron.weekly,
/etc/cron.monthly– directories for scripts run at the corresponding intervals.
crontab Usage
Common commands:
<code>crontab [-u username] // omit -u to operate on current user
-e // edit the crontab
-l // list current crontab entries
-r // remove the crontab</code>Each crontab line consists of a time specification (minute hour day month weekday) followed by the command to execute. The following operators are supported:
*– every possible value
/– step values (e.g., every 5 minutes)
-– range of values
,– list of specific values
Examples:
<code>0 0 25 12 * // at 00:00 on December 25
*/5 * * * * // every 5 minutes
* 4-6 * * * // at 4, 5, and 6 o'clock daily
* * * * 2,5 // every Tuesday and Friday</code>Combining with Simple Shell Scripts
For complex logic, place commands in a shell script and invoke the script from
crontab.
<code>#!/bin/sh
a="hello world"
echo $a</code>Add the script to the crontab, e.g.:
*/5 * * * * /usr/sh/test.shOr run a PHP script via:
/phppath/php /filepath/test.phpIf you found this article helpful, feel free to follow or comment with any questions.
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.