Linux Basics Lesson 38 of 38
The backup did not run
10 minutes
Learning goals
- Follow a cron job from the crontab through the journal to its own log.
- Find the cause of "not found" in the environment of cron.
- Repair the crontab with the full path, and check it afterwards.
Three questions, one after the other: is the job there, did cron start it, and what did it say? Then you fix it, and check that the fix is in place.
The script lies in ~/bin/backup.sh. You can read it with head. It does not run on this page: ./bin/backup.sh gives a hint, because running scripts comes with another package, and backup.sh alone answers command not found, because ~/bin is not in your PATH here. cat, ls and sudo are not loaded either: cat and ls answer with command not found, sudo with a hint. head, tail, journalctl and crontab are all you need.
The crontab has no editor here. Write the new one with echo into a pipe to crontab -, or into a file that you install with crontab FILE.
Steps
Step 1/5: First question. Is the job in the crontab at all? Show it. Step 2/5: Second question. Did cron start the job at 03:00? Look in the journal of the service cron around that time. Step 3/5: Third question. What did the job say? Read the log that the crontab line writes into. Step 4/5: Fix it. The job should run every day at 03:00 with the full path /home/lerner/bin/backup.sh, and its output still goes into the log. No line with backup.sh without a path may stay active. Step 5/5: Check it. Show the crontab as cron will read it tonight. Scenario solved
08:45, and the backup of last night is missing from ~/backup. The newest file there is from the 15th. Mia set up the nightly job yesterday, and she says that backup.sh runs cleanly when she starts it by hand. So what happened at 03:00?
-
Step 1 done solved before
First question. Is the job in the crontab at all? Show it.
Hint 1: crontab lists your jobs with one option.Hint 2: Type crontab -lSolution
crontab -lWhy: 0 3 * * * backup.sh >> /home/lerner/logs/backup.log 2>&1: every day at 03:00, with the output going into a log. The time is right. backup.sh without a path is the first thing that catches the eye.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 2 done solved before
Second question. Did cron start the job at 03:00? Look in the journal of the service cron around that time.
Hint 1: -u picks the service cron, --since and --until frame the time.Hint 2: Type journalctl -u cron --since 02:55 --until 03:05Solution
journalctl -u cron --since 02:55 --until 03:05Why: CRON[8812]: (lerner) CMD (backup.sh ...) at 03:00:01. cron did start it, on time and as lerner. So the schedule is fine, and the problem lies in the command itself.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 3 done solved before
Third question. What did the job say? Read the log that the crontab line writes into.
Hint 1: The path is at the end of the crontab line, after >>. cat is not on this page, head and tail are.Hint 2: Type head ~/logs/backup.logSolution
head ~/logs/backup.logWhy: /bin/sh: 1: backup.sh: not found. cron started the job, but its shell did not find the script. cron runs with PATH=/usr/bin:/bin, and ~/bin is not in it.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 4 done solved before
Fix it. The job should run every day at 03:00 with the full path /home/lerner/bin/backup.sh, and its output still goes into the log. No line with backup.sh without a path may stay active.
Hint 1: Write the whole new crontab, with the full path in front of backup.sh. echo with quotes into a pipe, and crontab - reads it.Hint 2: Type echo '0 3 * * * /home/lerner/bin/backup.sh >> /home/lerner/logs/backup.log 2>&1' | crontab -Solution
echo '0 3 * * * /home/lerner/bin/backup.sh >> /home/lerner/logs/backup.log 2>&1' | crontab -Why: The new line no longer depends on any PATH. Adding the right line next to the old one would not have been enough, because the old one would fail again every night and fill the log with not found.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 5 done solved before
Check it. Show the crontab as cron will read it tonight.
Hint 1: The same command as in the first step.Hint 2: Type crontab -lSolution
crontab -lWhy: One line, with the full path and the log. Whether tonight works, you see tomorrow morning with journalctl -u cron and in the log. On a real server you would run the script once by hand, exactly as cron will, with /home/lerner/bin/backup.sh.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Scenario solved
Why each step mattered
- Step 1: 0 3 * * * backup.sh >> /home/lerner/logs/backup.log 2>&1: every day at 03:00, with the output going into a log. The time is right. backup.sh without a path is the first thing that catches the eye.
- Step 2: CRON[8812]: (lerner) CMD (backup.sh ...) at 03:00:01. cron did start it, on time and as lerner. So the schedule is fine, and the problem lies in the command itself.
- Step 3: /bin/sh: 1: backup.sh: not found. cron started the job, but its shell did not find the script. cron runs with PATH=/usr/bin:/bin, and ~/bin is not in it.
- Step 4: The new line no longer depends on any PATH. Adding the right line next to the old one would not have been enough, because the old one would fail again every night and fill the log with not found.
- Step 5: One line, with the full path and the log. Whether tonight works, you see tomorrow morning with journalctl -u cron and in the log. On a real server you would run the script once by hand, exactly as cron will, with /home/lerner/bin/backup.sh.
Path complete
That was the last case of Linux basics. Ten modules: finding your way on a server, files and folders, text tools, pipes and redirection, permissions and sudo, processes, memory, disks, the network and the everyday work of an admin. Each module ends with a module check on its page, a good way to see what has stuck. LPIC-1 comes later and builds on all of this.
On to the module check Back to the path
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Loading terminal …
Esc, then Tab leaves the terminal
Step 1 of 5
Cards
-
Card 1 of 2
Tap the card or press Space to turn it over.
-
Card 2 of 2
Tap the card or press Space to turn it over.
Cheat sheet
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 67 KB)