Did I do this correctly?
crontab is a little difficult to understand how to set a job.
# Do a cleanup at 10 am on the 1st of every month
0 10 1 * * /home/andy/bin/CleanUp.sh
Did I do this correctly?
crontab is a little difficult to understand how to set a job.
# Do a cleanup at 10 am on the 1st of every month
0 10 1 * * /home/andy/bin/CleanUp.sh
It looks OK to me, but I'll outline some things that you should make sure of.
First, I hope you didn't put that line in /etc/crontab
. Two things can bite you if you do that: First, you'd be missing a field, specifically the user to run the script as; and second, if you specify root
as the user, then you have a security risk (any program running under your normal user's privileges can modify your script and thus run arbitrary commands as root
). So I'm assuming you put this into a file named /var/spool/cron/crontabs/andy
-- that's where your user's own crontab would be located. If you did that, then that line should work just fine.
Another issue you might encounter: Make sure that script (/home/andy/bin/CleanUp.sh
) is marked as executable -- otherwise, cron
won't run your script at 10 each morning, it'll probably say something cryptic like "Job failed" in one of those log files that nobody looks at regularly. Make sure the execute bit is on:
$ ls -l /home/andy/bin/CleanUp.sh
-rwxr-xr-x 1 Aug 5 2022 /home/andy/bin/CleanUp.sh
Obviously, if ls
didn't show those x
characters at the left, you should set the execute bit:
$ chmod +x /home/andy/bin/CleanUp.sh
One final note: If unsure whether the job will run, change the time specification in your crontab for testing purposes (and then obviously change it back when you're done testing). Keep in mind that cron
only checks for new entries in the crontabs every minute at best, so if the current time is 4:30 PM, I'd suggest setting the job to run at 4:32 to be safe:
32 16 * * * /home/andy/bin/CleanUp.sh
Obviously, as I already said, set the crontab back to 10 AM when you're done testing. You'd hardly believe how much hair I've lost to screw-ups like that. (Another reason I avoid using cron
like the plague, and actually religiously avoid having it installed on my Linux systems when I can avoid it. For me it's just too much work and worry, and I've got better solutions -- but cron
is necessary for some repetitive tasks, so I respect it when other people use it.)
see https://crontab.guru/ quite useful.
What you show is:
*βAt 10:00 on day-of-month 1.β*
at 2022-09-01 10:00:00
then at 2022-10-01 10:00:00
then at 2022-11-01 10:00:00
then at 2022-12-01 10:00:00
Can you be more specific with this statement.
First, you'd be missing a field, specifically the user to run the script as;
I did put it in `/var/spool/cron/crontabs/andy.
#!/bin/bash
# CleanUp.sh
#
# Run this periodically to clean up unneeded files
#
clear
echo fake | sudo -S apt -y update
echo fake | sudo apt-get dist-upgrade
echo fake | sudo -S apt-get clean
# autoclean clears out the local repository of retrieved package files
# Local repository of package files are in /var/cache/apt/archives/
echo marlin | sudo -S apt autoremove
Good, you don't need to worry about that extra field since you're using a user crontab, instead of the system-wide crontab. You're good to go!
Thanks so much gordon for your help.