Cron/Anacron in Linux

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
In Linux I am using cron/anacron to schedule a script to run periodically. It is my first attempt, and it has run successfully for some time.
The script is placed in cron.weekly.
But after I modified the script in a minor way it will not run in cron weekly. It does run OK from the command line.
I see the script in the cron.weekly directory, but despite searching exhaustively I cannot find how to bring up the schedule to check and alter the time
settings.
So two queries :
1. how does one examine and edit the schedule details for a job in cron.weekly?
2. should I move the script again into cron.weekly (i.e. after the small mods. that I made) to try to get it to run in cron.weekly?
(Note - the script has the correct permissions, and no "sh" suffix.)
Thanks in advance for any help.
Tom.
 

managed

Allan
Trusted Advisor
Spam Fighter
Joined
May 24, 2003
Messages
14,235
Please tell us the Linux distro and version you're using and post the old and new scripts and your crontab file.
 

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
Gentlemen, many thanks for your interest in my little problem.
1. The script does work if I run it from the command line. ( Perhaps it was not clear but I did affirm this in my original post.)
2. I am running Debian 10 Buster with the Cinnamon desktop. It is all up to date.
3. I cannot present the old script since I modified it (and then reverted to the original or something closely approximating it) to become the present version.
4. The script (pkgslog) file is.....
-----
#!/bin/sh
#Script for Anacron weekly update of list of installed apps. Sends to /documents/pkglst.txt
set -e
PATH=$PATH:/home/drtjpike/bin
touch /home/drtjpike/Documents/pkglst.txt
truncate -s 0 /home/drtjpike/Documents/pkglst.txt
DT=$(date +%d/%m/%y)
dpkg --get-selections > /home/drtjpike/Documents/pkglst.txt
# > /dev/null 2>&1
---
5. The crontab file is.....
-----
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/drtjpike/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
6. I put the script in cron.weekly.....
------
0anacron man-db pkgslog
_____

As I remarked, I cannot for all the tea in China find how to view and edit the cron.weekly details to check them. I find it interesting that when I issue the command crontab -l there are only the commented-out instructions- no jobs. And no crontab for root.

I hope that you are able to make some sense of this. Thanks again, Tom.
 

managed

Allan
Trusted Advisor
Spam Fighter
Joined
May 24, 2003
Messages
14,235
I've read there are 2 types of cron job, system-wide or user-based, which may have some bearing on your issue.

See here :- https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804
And here :- https://linuxhint.com/setup_cron_jobs_linux/

Hopefully something at those links will help, I think the Ubuntu link should also apply to Debian.

FWIW I set up a cron job in lubuntu like this :-

Open a Terminal (Ctrl-Alt-T)
type : crontab -e
choose #2 nano editor (default)
append : @reboot /home/lubuntu/mm.sh
press Ctrl+O (letter O not zer0) then Enter to write the file
press Ctrl+X (letter O not zer0) then Enter to exit nano

You could try a similar method. You get offered a list of editors the first time you run crontab -e, if you run it again later it opens that editor automatically.
Mine above runs at every boot (@reboot) and because it's in the home folder (actually /home/lubuntu/) it is already in the Path. It runs a very small script.
You would have to add your path and change it to weekly of course.
 
Last edited:

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
Allan : many thanks for your input.
The two links that you cited are familiar to me. Like so many others they dwell on the time settings as if this aspect is rocket science. But there is nothing to help me with my queries unfortunately.
I do know that cron jobs can be system wide (run by root) or user specific. In my case I put the script in cron.weekly which I understand is run under run-parts which run as root jobs.
But cron -l says there are no root jobs in my system.
I do know how to create new cron jobs with crontab -e. But my problems concern an existing cron.weekly job that does not run (except 'manually' from the command line), and I cannot find out how to view the details of its settings.
Thanks again for your interest, Tom.
 

managed

Allan
Trusted Advisor
Spam Fighter
Joined
May 24, 2003
Messages
14,235
I was thinking you could set up a new cron job like I did in my previous post and see if that works.
 

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
I set up a new job in anacrontab. It was a simple hello script, set to run weekly.
It ran successfully today.
It is strange to me that the script that I want to run,but does not, does not appear in anacrontab, but it is shown when I list the contents of cron.weekly .
Thanks for your help so far. Any more thoughts for a solution please?
Tom.
 

managed

Allan
Trusted Advisor
Spam Fighter
Joined
May 24, 2003
Messages
14,235
Looking online it seems cron jobs will only run when the computer is actually on when the job becomes due, whereas anacron will run the job the next time the computer is switched on even if the job's scheduled time was in the past. Could that be relevant to your issue ?
 

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
Looking online it seems cron jobs will only run when the computer is actually on when the job becomes due, whereas anacron will run the job the next time the computer is switched on even if the job's scheduled time was in the past. Could that be relevant to your issue ?
Thanks. Yes it is relevant. I chose anacron for the task specifically because it runs jobs after a restart that were timed to run when a computer is shut down.
Thanks for your input.
Tom.
 

DocTomJP

Thread Starter
Joined
Jun 5, 2016
Messages
10
People, many thanks to all who contributed to this thread.
It appears that there is no obvious solution to my events, so it is probably time to move on.
I hesitate to declare the issue as solved, but it seems that is the only way to close a thread, so here goes! Thanks again.
Tom.
 

managed

Allan
Trusted Advisor
Spam Fighter
Joined
May 24, 2003
Messages
14,235
Sorry I couldn't help more, I do wonder if the job should be in cron.weekly though, maybe that's the wrong place for it ?

Any how good luck with your Linux. (y)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top