Master Linux Automation: Startup Scripts, at, and Cron Scheduling
This guide explains how to automate common Linux tasks such as configuring boot‑time services with chkconfig, creating one‑time jobs using the at command, and scheduling recurring jobs with cron, including syntax, run‑level concepts, and example shell scripts.
Boot‑time startup
Use chkconfig to manage services across run‑levels (0–6). Run‑levels: 0 shutdown, 1 single‑user, 2 multi‑user without NFS, 3 multi‑user with NFS, 4 unused, 5 graphical, 6 reboot.
chkconfig --list # list services and their run‑level status
chkconfig --add myservice # add a new service
chkconfig --level 2,3,4,5 myservice on # enable at levels 2‑5
chkconfig --del myservice # remove serviceAlternatively edit scripts under /etc/rc.d/ or add commands to /etc/rc.local, e.g. /usr/local/apache/bin/apachectl start to launch Apache on boot.
One‑time tasks with at
The at utility schedules a single execution. After invoking at with a time specification, enter commands, finish with EOF (Ctrl‑D).
# at 14:30 tomorrow
at> /usr/bin/backup.sh
at> ^DCommon time formats:
at 14:30 tomorrow at now + 10 minutes at midnight at 9:00 pmEnsure the atd daemon is running; start it if disabled.
Recurring tasks with cron
The cron daemon reads crontab files. User crontabs are in /var/spool/cron/; system crontab is /etc/crontab and /etc/cron.d/. Directories /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly run scripts at fixed intervals.
Cron syntax
A crontab line consists of five time fields (minute hour day‑of‑month month day‑of‑week) followed by the command. Special characters: * – every possible value / – step values (e.g., */5 every 5 units) - – range (e.g., 1-5) , – list of specific values (e.g., 1,3,5)
# Example entries
0 0 25 12 * # Dec 25 at 00:00
*/5 * * * * # every 5 minutes
* 4-6 * * * # at 04:00, 05:00, 06:00 daily
* * * * 2,5 # every Tuesday and FridayUsing shell scripts with cron
Place complex logic in a script and invoke it from crontab. Example script /usr/sh/test.sh:
#!/bin/sh
a="hello world"
echo $aAdd to crontab: */5 * * * * /usr/sh/test.sh PHP scripts can be scheduled similarly, e.g. /usr/bin/php /path/to/script.php.
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.
