Essential Linux Service Management Techniques

Crontab Scheduled Tasks

In Linux, the crontab utility allows users to schedule commands to run at specific intervals. Each line in a crontab file represents a scheduled task with six fields: five for time specification and one for the commend to execute.

The format is as follows:

minute hour day month week command

Where:

  • minute: An integer between 0-59 representing the minute
  • hour: An integer between 0-23 representing the hour
  • day: An integer between 1-31 representing the day of the month
  • month: An integer between 1-12 representing the month
  • week: An integer between 0-7 representing the day of the week (0 or 7 denotes Sunday)
  • command: The command or script to be executed

Special characters can be used in these fields:

  • Asterisk (*): Matches all possible values
  • Comma (,): Specifies a list of values (e.g., "1,2,5,7,8,9")
  • Hyphen (-): Defines a range (e.g., "2-6" means "2,3,4,5,6")
  • Forward slash (/): Specifies step values (e.g., "0-23/2" means every 2 hours)

Create a sample script at /var/www/scripts/task.py:

print("Task executed successfully")

Open the crontab editor:

crontab -e

Add a line to execute the script every minute, appending output to /var/log/task.log:

*/1 * * * * python /var/www/scripts/task.py >> /var/log/task.log

Monitor the log file:

tail -f /var/log/task.log

List all scheduled tasks:

crontab -l

To run the script at 10 minutes past every hour:

10 * * * * python /var/www/scripts/task.py >> /var/log/task_hourly.log

Ntpdate Time Synchronization

Install the NTP client if not already present:

yum install ntp

Synchronize system time with a NTP server and set the timezone to Asia/Shanghai:

ntpdate cn.pool.ntp.org
rm /etc/localtime 
sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Logrotate Log Management

Navigate to the logrotate configuration directory:

cd /etc/logrotate.d/

View existing configuration files:

ls -la

Examine the Nginx log rotation configuration:

cat nginx

Manually trigger log rotation for Nginx:

logrotate /etc/logrotate.d/nginx

Check the Nginx log directory to verify rotation:

ls -la /var/log/nginx

Supervisor Process Management

Install Supervisor using pip:

sudo pip install supervisor

If pip is not available, install it first:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

Create the Supervisor configuration directory and generate the main configuration file:

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisor.conf

Edit the configuration file:

vi /etc/supervisor/supervisor.conf

Modify the include section at the end of the file:

[include]
files = /etc/supervisor/conf.d/*.ini

Create a directory for application configurations:

mkdir -p /etc/supervisor/conf.d

Create a configuraton file for Redis:

vi /etc/supervisor/conf.d/redis.ini

Add the following configuration:

[program:redis]
command=/usr/local/bin/redis-server 
autostart=true
autorestart=true
startsecs=3

Start the Supervisor daemon:

supervisord -c /etc/supervisor/supervisor.conf

Verify that Supervisor is running:

ps -ef | grep supervisord

Tags: crontab ntpdate logrotate Supervisor Linux

Posted on Fri, 06 Nov 2026 16:14:33 +0000 by herando