Systemd and Crontab in Linux

zhi tao
3 min readAug 9, 2023

Systemd

Authorize shell script

The Bash script content, you can do any thing you want here.

#!/bin/bash
# in /home/admin/start.sh
set -e
ps aux | grep test| grep -v grep | awk '{print $2}' | xargs kill -9 || true
mkdir -p /home/admin/logs
java -jar /home/admin/test.jar \
-Xms5334m -Xmx5334m -Xmn2000m -XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=1g \
-XX:SurvivorRatio=10 -XX:+UseConcMarkSweepGC \
--spring.profiles.include=default,prod > /home/admin/logs/error.log

Chmod it.

  • chmod 755 /home/admin/start.sh

Create service

  • vim /etc/systemd/system/test.service
[Unit]
Description = test systemd
After = network.target

[Service]
ExecStart = /home/admin/start.sh
Restart = on-abort

[Install]
WantedBy = multi-user.target
  • chmod 664 /etc/systemd/system/test.service

Reload

  • sudo systemctl enable test.service
  • sudo systemctl daemon-reload

List

  • systemctl --type=service --all

Start

  • systemctl start test

Status

  • systemctl status test

CMD

Reboot System

  • check whether the service start automatically or not

Crontab

Authorize shell script

  • chmod 755 /home/admin/start.sh

Create

crontab -e

write the following content:

* */1 * * * /home/admin/start.sh

it means “execute /home/admin/start.sh every hour"

Linux Crontab Format

MIN HOUR DOM MON DOW CMD

Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

| Field | Description  | Allowed  Value |
| ----- | ------------ | -------------- |
| MIN | Minute | 0 to 59 |
| HOUR | Hour field | 0 to 23 |
| DOM | Day of Month | 1-31 |
| MON | Month field | 1-12 |
| DOW | Day Of Week | 0-6 |
| CMD | Command | Any command to be executed. |

Examples:

30 08 10 06 * /home/ramesh/full-backup
  • 30–30th Minute
  • 08–08 AM
  • 10–10th Day
  • 06–6th Month (June)
  • * — Every day of the week
00 11,16 * * * /home/ramesh/bin/incremental-backup
  • 00–0th Minute (Top of the hour)
  • 11,16–11 AM and 4 PM
  • * — Every day
  • * — Every month
  • * — Every day of the week
00 09-18 * * * /home/ramesh/bin/check-db-status
  • 00–0th Minute (Top of the hour)
  • 09–18–9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * — Every day
  • * — Every month
  • * — Every day of the week

The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.

  • When you specify */5 in minute field means every 5 minutes.
  • When you specify 0–10/2 in minute field mean every 2 minutes in the first 10 minute.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Table: Cron special keywords and its meaning


| Keyword | Equivalent |
| ------- | --------------- |
| @yearly | 0 0 1 1 * |
| @monthly| 0 0 1 ** * |
| @daily | 0 0 * * * |
| @hourly | 0 * * * * |
| @reboot | Run at startup. |

Check

crontab -l

to check the schedule

Check the process

ps -ef | grep java

Example

execute shell every two minutes

References

--

--