Operations 9 min read

Master Linux Automation: Startup Scripts, at, and Cron Made Easy

This guide explains how to automate common Linux tasks such as configuring startup services with chkconfig and rc.local, scheduling one‑off jobs using at, and setting recurring jobs with crontab, including command syntax, examples, and best‑practice tips for reliable system maintenance.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Automation: Startup Scripts, at, and Cron Made Easy

When a Linux‑based website is in production, administrators often need to perform maintenance tasks such as checking resource usage, rotating logs, and organizing data. Automating these tasks saves manpower, avoids peak‑hour interference, improves accuracy, and reduces the need for manual command entry.

Save labor – a single script can handle the job.

Run at night to avoid traffic spikes.

Accurate execution when properly configured.

Peace of mind – no need to type commands repeatedly.

Startup Automation

Automatically launching services or processes at boot is a common requirement. Using chkconfig you can enable services for specific runlevels.

Linux runlevels:

0 – halt

1 – single‑user mode

2 – multi‑user, no NFS

3 – multi‑user with NFS

4 – unused

5 – graphical multi‑user

6 – reboot

Typical chkconfig commands:

chkconfig --list               # show current startup list
xxxd 0:off 1:off 2:on ... 6:off   # example output, service "xxxd" starts at levels 2‑5
chkconfig --add xxxd           # add service to list
chkconfig [--level 1-6] xxxd on|off   # enable/disable at specified levels (default 2‑5)
chkconfig --del xxxd          # remove service from list

You can also edit files under /etc/rc.d/ directly. The rc.local file is a convenient place to add custom startup 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 at a specified time.

# at time          # specify when to run
at> operation      # type the command(s) to execute
at> Ctrl+D        # finish editing

Common time formats:

at H:m tomorrow                # next day at hour H minute m
at now + n minutes|hours|days|weeks   # n units from now
at midnight                     # at midnight
at H:m pm/am                    # today at H:m in the specified period

The scheduled 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 crontab

The built‑in cron daemon handles periodic tasks. Cron jobs are defined in a crontab file, which consists of time fields followed by a command.

Cron Overview

Typical locations for crontab files: /var/spool/cron/ – per‑user crontabs (including root) /etc/crontab – system‑wide schedule /etc/cron.d/ – additional crontab snippets /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly – directories for hourly/daily/weekly/monthly scripts

Using crontab

Common commands:

crontab [-u username]   # operate on the specified user's crontab
  -e   # edit the crontab (opens in vim)
  -l   # list current entries
  -r   # remove the crontab

A crontab line has five time fields (minute, hour, day of month, month, day of week) followed by the command. Special symbols: * – every possible value / – step values (e.g., */5 every 5 units) - – range (e.g., 4-6 for 4 through 6) , – list of specific values

Examples:

# minute hour day month weekday   command
0 0 25 12 *   # run at midnight on Dec 25
*/5 * * * *   # every 5 minutes
* 4-6 * * *   # at 4, 5, and 6 am each day
* * * * 2,5   # every Tuesday and Friday

Combining 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 $a

Add the script to the crontab, e.g.: */5 * * * * /usr/sh/test.sh Or run a PHP script via the PHP interpreter: /phppath/php /filepath/test.php If you found this tutorial helpful, feel free to share it with others.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationLinuxcron
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.