Open Root’s Crontab
sudo crontab -e
Select Editor (if Prompted)
- If this is the first time running crontab -e as root, you’ll see a prompt to choose an editor (e.g., nano, vim). You mentioned “select option 1,” which might mean choosing nano (often option 1). For example:
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim
- Press 1 and Enter if you want nano. If you’ve already set this, it’ll skip to the editor directly.
Insert the Cron Job Line
0 0 * * * /home/user/script/backup.sh > /dev/null 2>&1
Save and Exit
-
For nano:
Press Ctrl + O, then Enter to write the file.
Press Ctrl + X to exit. -
For vim:
Press i to enter insert mode, add the line.
Press Esc, then type :wq and Enter to save and quit.
You’ll see a message like:
crontab: installing new crontab.
Verify (Optional)
Check that it’s set:
sudo crontab -l
Explanation of “>/dev/null 2>&1”
- “>”: This is a redirection operator in Bash. It sends output somewhere.
- “/dev/null”: This is a special file in Unix-like systems that acts as a “black hole.” Anything sent to /dev/null is discarded and disappears.
- “2>&1”: This redirects standard error (stderr, file descriptor 2) to the same place as standard output (stdout, file descriptor 1).
Together, >/dev/null 2>&1 means:
- Standard output (stdout) from the script (e.g., anything it prints) is sent to /dev/null and discarded.
- Standard error (stderr) (e.g., error messages) is also redirected to /dev/null and discarded.
In short, this ensures the script runs silently—neither its normal output nor its errors will be saved or emailed to you (cron typically emails output to the user if not redirected).
online crontab generator
https://crontab-generator.org/
show the crontab excution record
sudo cat /var/log/syslog | grep CRON
评论区