Scheduling jobs & reading logs
A server does a lot of work when nobody is logged in: backups at night, cleanup every hour, health checks every few minutes. You schedule that work with cron or systemd timers, and you find out whether it worked by reading logs.
The crontab format
Each user has a crontab, a list of jobs. Every line is five time fields followed by the command to run:
| Field | Allowed values |
|---|---|
| minute | 0–59 |
| hour | 0–23 |
| day of month | 1–31 |
| month | 1–12 (or jan–dec) |
| day of week | 0–7, where 0 and 7 are both Sunday (or sun–sat) |
In each field * means every value, 1,15 is a list, 1-5 a range and */10 a step ("every 10th").
*/5 * * * * every 5 minutes
0 * * * * at the start of every hour
30 2 * * * every day at 02:30
0 9 * * 1-5 09:00 Monday to Friday
0 0 1 * * midnight on the 1st of every month
0 */6 * * * every 6 hours (00:00, 06:00, 12:00, 18:00)
@reboot once, when cron starts after a boot
@daily same as 0 0 * * * (also @hourly, @weekly, @monthly)
Times are in the server's time zone (check with timedatectl), which on cloud servers is often UTC. One oddity: if you restrict both day of month and day of week, the job runs when either matches, so 0 0 13 * 5 means every 13th and also every Friday.
Editing crontabs
$ crontab -e # edit your jobs (opens an editor, installs on save)
$ crontab -l # list your jobs
$ sudo crontab -u deploy -l # another user's jobs
$ crontab -r # DELETES your whole crontab without asking. Careful: -r is next to -e
There are also system-wide places: /etc/crontab and files in /etc/cron.d/ have an extra field for the user to run as (0 3 * * * root /usr/local/bin/backup). Scripts dropped into /etc/cron.daily/, cron.hourly, cron.weekly or cron.monthly run on that schedule with no time fields at all, but on Debian and Ubuntu their file names may only contain letters, digits, - and _: a file called backup.sh there is silently skipped.
Cron's environment: why jobs work in the terminal but not in cron
Cron does not load your ~/.bashrc or ~/.profile. Jobs run with a tiny environment:
PATHis only/usr/bin:/bin, so anything in/usr/local/bin,~/.local/binor a virtual environment is "not found". Use full paths, or setPATH=...on a line at the top of the crontab.- The shell is
/bin/sh(dash on Ubuntu), not bash. Put bash syntax in a script with a bash shebang and call the script. - Your exported variables (API keys and so on) don't exist. Set them in the crontab, or have the script load them from a file.
- The working directory is your home folder, so relative paths in the script point there. Use absolute paths or
cdfirst. %is special in a crontab line (it means "newline").date +%Fmust be writtendate +\%F.
Output of a job is emailed to you, but only if the server has a mail program; on a typical Ubuntu server it isn't installed and the output is thrown away. Always redirect it to a log file so you can see what happened:
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
# every 5 minutes, keep both output and errors
*/5 * * * * /home/demee/bin/healthcheck.sh https://demee.cc >> /home/demee/logs/health.log 2>&1
# nightly backup; flock -n skips this run if the previous one is still going
0 3 * * * flock -n /tmp/backup.lock /home/demee/bin/backup.sh /srv/app >> /home/demee/logs/backup.log 2>&1
To check whether cron ran a job at all, look for its CMD lines: journalctl -u cron --since today or grep CRON /var/log/syslog.
systemd timers, briefly
A timer is systemd's alternative to cron. You write two small unit files: a .service that says what to run and a .timer that says when.
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
User=demee
ExecStart=/home/demee/bin/backup.sh /srv/app
# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup every night
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now backup.timer
$ systemctl list-timers # all timers, with next and last run
$ systemd-analyze calendar "Mon..Fri *-*-* 09:00" # check an OnCalendar expression
Compared with cron, the output lands in the journal automatically (journalctl -u backup), Persistent=true runs a missed job after the machine was off, and systemctl start backup.service runs it on demand for testing. Ubuntu itself uses timers for jobs like log rotation.
Reading logs with journalctl
systemd collects the output of every service, plus kernel and system messages, in the journal. journalctl reads it; newest entries are at the bottom.
$ journalctl -u nginx # one service (-u = unit)
$ journalctl -u nginx -n 50 # just the last 50 lines
$ journalctl -u nginx -f # follow live, like tail -f
$ journalctl -u nginx --since "1 hour ago"
$ journalctl --since "2026-09-27 09:00" --until "2026-09-27 10:00"
$ journalctl -p err -b # errors and worse, since this boot
$ journalctl -u myapp -g "timeout" # only lines matching a pattern
$ journalctl -k # kernel messages (disks, out-of-memory kills)
$ journalctl --disk-usage # how much space the journal uses
$ sudo journalctl --vacuum-time=14d # delete journal data older than 14 days
Priorities from most to least severe: emerg (0), alert, crit, err, warning, notice, info, debug (7). -p warning shows warning and everything more severe. If you only see your own messages, use sudo or join the adm or systemd-journal group.
Classic log files in /var/log
| File | What's in it |
|---|---|
/var/log/syslog | general system messages (Debian/Ubuntu; /var/log/messages on Red Hat family) |
/var/log/auth.log | logins, SSH, sudo use (/var/log/secure on Red Hat family) |
/var/log/kern.log | kernel messages |
/var/log/apt/history.log, dpkg.log | what was installed or upgraded, and when |
/var/log/nginx/access.log, error.log | each web request, and web server errors |
$ sudo tail -f /var/log/auth.log # watch logins live
$ sudo grep "Failed password" /var/log/auth.log | tail
$ ls /var/log/syslog*
/var/log/syslog /var/log/syslog.1 /var/log/syslog.2.gz ...
$ zgrep -i "error" /var/log/syslog.2.gz # grep inside a compressed old log
$ zless /var/log/syslog.2.gz
logrotate: stopping logs from filling the disk
Those .1 and .gz files come from logrotate, which runs once a day (on Ubuntu via logrotate.timer). It renames the current log, starts a fresh one, compresses old ones and deletes the oldest. Packages put their rules in /etc/logrotate.d/; add one for your own app:
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
copytruncate
}
$ sudo logrotate -d /etc/logrotate.d/myapp # dry run: show what would happen
$ sudo logrotate -f /etc/logrotate.d/myapp # force a rotation now to test
daily: rotate every day (orweekly,monthly,size 100M).rotate 14: keep 14 old files and delete anything older.compressgzips old files;delaycompressleaves the newest old one (.1) uncompressed for one more round.missingok: no error if the log doesn't exist;notifempty: don't rotate an empty file.copytruncate: copy the log, then empty the original, for apps that keep the file open.
Comments in logrotate files must be on their own line: rotate 14 # keep two weeks is an error ("bad rotation count"). copytruncate is the simple choice when the app can't be told to reopen its log file; a few lines written during the copy can be lost. The alternative is create plus a postrotate script that tells the app to reopen its log. If logrotate complains that a directory has "insecure permissions", add a line like su myapp myapp to the rule so it knows which user to rotate as.