Master Linux Automation: Startup Scripts, chkconfig, at & crontab Explained
This guide explains how to automate Linux tasks by configuring boot‑time startup with chkconfig and rc.local, using the one‑time scheduler at, and setting up recurring jobs with crontab, including runlevel basics, command syntax, and practical script examples.
Why Automate?
Automation reduces manual effort, avoids peak‑hour load, minimizes human error, and ensures repeatable execution of routine tasks.
Boot‑time Service Startup
Linux uses runlevels to define system states. The traditional SysV runlevels are:
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
The chkconfig utility manages which services start in which runlevels.
chkconfig --list # List services and their runlevel settings</code>
<code>chkconfig --add <em>service</em> # Add a new service to the management list</code>
<code>chkconfig --del <em>service</em> # Remove a service from the list</code>
<code>chkconfig [--level 1-6] <em>service</em> on|off # Enable or disable for specific levels (default 2‑5)Typical output shows the enabled levels, e.g. xxxd 0:off 1:off 2:on 3:on 4:on 5:on 6:off.
For simple cases you can edit /etc/rc.d/rc.local (or /etc/rc.local on newer distributions) with a text editor and add the exact command you want to run at boot, for example:
vim /etc/rc.d/rc.local</code>
<code>/usr/local/apache/bin/apachectl startAfter saving, ensure the script is executable:
chmod +x /etc/rc.d/rc.localOne‑time Scheduling with at
The at daemon runs a single command at a specified time. The daemon atd is often disabled by default; start it with:
systemctl start atd</code>
<code>systemctl enable atd # optional, to start at bootSchedule a job:
# at 14:30</code>
<code>at> /path/to/command</code>
<code>at> ^D # Ctrl+D to finishCommon time specifications:
at 09:15 tomorrow # next day at 09:15</code>
<code>at now + 10 minutes # 10 minutes from now</code>
<code>at midnight # today at 00:00</code>
<code>at 18:00 pm # today at 18:00Pending jobs are stored under /var/spool/at and can be listed with atq and removed with atrm.
Recurring Scheduling with crontab
The cron daemon executes tasks repeatedly according to a schedule defined in crontab files.
Configuration locations
/var/spool/cron/– per‑user crontabs (named after the user) /etc/crontab – system‑wide crontab (includes a user field) /etc/cron.d/ – additional crontab fragments /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly – directories for scripts run at the indicated interval
Basic crontab commands
crontab -e # Edit current user’s crontab (opens $EDITOR)</code>
<code>crontab -l # List current crontab entries</code>
<code>crontab -r # Remove the current crontabCrontab line syntax
A line consists of five time fields followed by the command: * * * * * command Field order: minute (0‑59), hour (0‑23), day of month (1‑31), month (1‑12), day of week (0‑7, where both 0 and 7 represent Sunday).
Special symbols: * – every possible value / – step values (e.g., */5 every 5 units) - – range (e.g., 4-6 for hours 4 through 6) , – list of discrete values (e.g., 2,5 for Tuesday and Friday)
Example entries
0 0 25 12 * # Run at 00:00 on December 25</code>
<code>*/5 * * * * # Every 5 minutes</code>
<code>* 4-6 * * * # Every minute during 04:00‑06:59 each day</code>
<code>* * * * 2,5 # Every minute on Tuesdays and FridaysRunning scripts from cron
For complex logic place the commands in an executable script and invoke the script from crontab. Example shell script:
#!/bin/sh</code>
<code>echo "Hello world"Make it executable: chmod +x /usr/local/bin/test.sh Then add to crontab: */5 * * * * /usr/local/bin/test.sh PHP scripts can be run similarly, specifying the interpreter:
*/5 * * * * /usr/bin/php /path/to/script.phpReference
Source URL: https://reurl.cc/v5o0xA
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.
