CtrlK

Fundamentals

Processes

Viewing processes

Our user or current user: ps
All users: ps aux
• List all services:
systemctl list-units --type=service OR
ps -aux
• View background processes
jobs

Network connection with associated ports: netstat -a

Kill processes: kill [process_id]

Start services: systemctl [option] [service]

Options:
start stop enable disable
Eg: systemctl start apache2

Background and foreground tasks in terminal

Background
At the end of the command, add: &
Or: Ctrl+z

Foreground
fg [id] - id is displayed after executing jobs

Services listening on system: ss -nltu

n: dont resolve the service
t, u: TCP and UDP
l: listening services

Scheduling processes and tasks

Systemd

  1. Create timer
    Script must contain:
    "Unit": Specifies a description for the timer.
    "Timer": Specifies when to start the timer and when to activate it.
    "Install": Specifies where to install the timer.
    Eg: mytimer.timer

[Unit]
Description=My Timer
[Timer]
OnBootSec=3min
OnUnitActiveSec=1hour
[Install]
WantedBy=timers.target

Run only once after boot: OnBootSec
Run regularly: OnUntiActiveSec

  1. Create service
    Set a description and specify the full path to the script we want to run.
    Eg: mytimer.service

[Unit]
Description=My Service

[Service]
ExecStart=/full/path/to/my/script.sh

[Install]
WantedBy=multi-user.target

  1. Activate timer
    Reload systemd: sudo systemctl daemon-reload
    Start the service: sudo systemctl start mytimer.timer
    Enable the service on boot: sudo systemctl enable mytimer.timer

• Cron

Store task in file called: crontab

Time FrameDescription
Minutes (0-59)This specifies in which minute the task should be executed.
Hours (0-23)This specifies in which hour the task should be executed.
Days of month (1-31)This specifies on which day of the month the task should be executed.
Months (1-12)This specifies in which month the task should be executed.
Days of the week (0-7)This specifies on which day of the week the task should be executed.

Eg:

# System Update
0 */6 * * * /path/to/update_software.sh
executed once every sixth hour. This is indicated by the entry 0 */6 in the hour column.

# Execute Scripts
0 0 1 * * /path/to/scripts/run_scripts.sh
executed every first day of the month at midnight. This is indicated by the entries 0 in the minute and hour columns and 1 in the days of the month column.

# Cleanup DB
0 0 * * 0 /path/to/scripts/clean_database.sh
executed every Sunday at midnight. This is specified by the entries 0 and 0 in the minute and hour columns and 0 in the days of the week column.

# Backups
0 0 * * 7 /path/to/scripts/backup.sh
executed every Sunday at midnight. This is indicated by the entries 0 and 0 in the minute and hour columns and 7 in the days of the week column.