Master Linux Automation: Startup Scripts, at, and Cron Scheduling
This guide explains how to automate common Linux tasks for web servers, covering boot‑time service startup with chkconfig and rc.local, one‑off scheduling using at, and recurring jobs with cron, including syntax, examples, and integration with shell scripts.
When operating a Linux‑based web site, routine maintenance such as monitoring resources, rotating logs, and performing data cleanup often needs to run automatically. This article introduces practical methods for automating typical Linux tasks.
Why Automate?
Save manpower – a single script can replace manual commands.
Run at night to avoid peak traffic and keep daytime performance unaffected.
Increase accuracy when the script is correctly configured.
Reduce the hassle of repeatedly typing commands.
Boot‑Time Startup
Automatically launching services or processes at system boot eliminates the need to manually start them each time.
Using chkconfig
Linux runlevels define the system state:
0 – halt
1 – single‑user mode
2 – multi‑user without NFS
3 – multi‑user with NFS
4 – unused
5 – multi‑user with graphical interface
6 – reboot
Typical chkconfig commands:
chkconfig --list // List current startup services
xxxd 0:off 1:off 2:on ... 6:off // Example output showing service "xxxd" starts at levels 2‑5
chkconfig --add xxxd // Add a new service
chkconfig [--level 1-6] xxxd on|off // Enable or disable at specific levels (default 2‑5)
chkconfig --del xxxd // Remove the serviceEditing rc.d files
Directly modify files under /etc/rc.d/. The rc.local script is a convenient place to add custom commands, e.g., /usr/local/apache/bin/apachectl start to launch Apache on boot.
One‑Time Scheduling with at
The at utility schedules a single execution of a command.
# at time // Start at command with a time argument
at> operation // Enter the command to run
at> Ctrl+D // End inputCommon time specifications:
at H:M tomorrow // Next day at hour H, minute M
at now + n minutes/hours/days/weeks // After n units
at midnight // At midnight
at H:M pm/am // Today at H:M in the specified periodScheduled jobs are stored in /var/spool/at. Note that the atd daemon is often disabled by default and must be started manually.
Recurring Scheduling with cron
The built‑in cron daemon handles periodic tasks. Cron jobs are defined in crontab files.
Cron Locations
/var/spool/cron/– per‑user crontabs (named after the user). /etc/crontab – system‑wide schedule. /etc/cron.d/ – additional crontab files. /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly – directories for scripts run at the corresponding intervals.
Using crontab
Common commands:
crontab [-u username] // Operate on a specific user's crontab
-e // Edit the crontab (opens in vim)
-l // List current entries
-r // Remove the crontabA crontab line consists of a time specification followed by the command. Time fields are minute, hour, day of month, month, and day of week. Special characters: * – every possible value / – step values (e.g., */5 every 5 units) - – range (e.g., 4-6 for hours 4 through 6) , – list of values (e.g., 2,5 for Tuesday and Friday)
Example entries:
# Minute Hour Day Month Weekday Command
0 0 25 12 * // Run at midnight on December 25th
*/5 * * * * // Every 5 minutes
* 4-6 * * * // Every minute during 4‑6 AM each day
* * * * 2,5 // Every minute on Tuesdays and FridaysCombining Cron with Shell Scripts
For complex logic, place the commands in a shell script and invoke the script from cron.
#!/bin/sh
# Simple example script
a="hello world"
echo $aAdd the script to crontab, e.g.: */5 * * * * /usr/sh/test.sh Or run a PHP script via cron: /phppath/php /filepath/test.php This article provides the essential commands and concepts needed to set up automated tasks on a Linux server, improving reliability and reducing manual effort.
Signed-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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
