Operations 9 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Master Linux Automation: Startup Scripts, at, and Cron Made Simple

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

chkconfig

utility 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

chkconfig

usage:

<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.local

with

vim

can add a line such as

/usr/local/apache/bin/apachectl start

to launch Apache on boot.

Using at for One‑Time Scheduled Tasks

The

at

command 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

atd

daemon is disabled by default and must be started manually.

Using crontab for Recurring Tasks

The built‑in

cron

daemon, together with

crontab

, handles periodic job scheduling.

Cron Overview

cron

is 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.sh

Or run a PHP script via:

/phppath/php /filepath/test.php

If you found this article helpful, feel free to follow or comment with any questions.

automationlinuxSysadminscriptingCronstartupat
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.