Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Cron.daily not running user scripts [SOLVED]

If none of the specific sub-forums seem right for your thread, ask here.
Message
Author
DocTomJP
Posts: 82
Joined: 2015-06-08 05:21
Location: Thailand
Been thanked: 1 time

Cron.daily not running user scripts [SOLVED]

#1 Post by DocTomJP »

I am sorry to have to appeal for more help to get my cron/anacron jobs to execute. But even the simplest script, written as a test, fails, and I have been round in circles and more in attempts to get them to run - without success. I guess that there may be something wrong with my attempts to write a script, or something to do with the path to the scripts. But I am at a loss, so I hope that some kind soul will come to my assistance.
I am attempting to get a script to run in cron.daily. The script is supposed to just echo "hello world" to an output file in my Documents directory. It works fine when I run it from the command line, but not from within cron.daily nor cron.weekly.

I have made sure that I have avoided some basic errors : the name does not contain any forbidden characters(., etc), the laptop runs on mains power, the script has execute permission.
I put a full PATH statement within the script. (Previously on advice from forum members I created an .xsessionrc file and included a PATH statement.)
Despite all this, the scripts will not run as cron jobs. Thus using run-parts --report /etc/cron.daily I get the output ...

sudo run-parts --report /etc/cron.daily
/etc/cron.daily/HiWorld:
run-parts: failed to exec /etc/cron.daily/HiWorld: No such file or directory
run-parts: /etc/cron.daily/HiWorld exited with return code 1.

So it appears that the system scripts (0anacron,dpkg, passwd, aptitude and so on) are running successfully, but mine is not.

I have tried placing the script in ~/bin, /bin, usr/bin, all to no avail.

The simple script is ......

Code: Select all

bin/sh
set -e 
PATH=/usr/local/bin:/usr/bin:/bin:~/bin
echo "hello world" > /home/drtjpike/Documents/TestHiW 2>&1
Please can someone help and point out my (no doubt stupid) error and show me how to get the cron jobs working?
Tom.
(Note I am running Debian 10 with the Cinnamon desktop.)
Last edited by DocTomJP on 2020-03-05 07:32, edited 2 times in total.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#2 Post by Head_on_a_Stick »

You should post the actual {ana,}cron job as well as the script. And call the full paths rather than relying on PATH.

Also, the shebang at the top of the script is wrong — it should be

Code: Select all

#!/bin/sh
Anyway I would use a systemd .timer unit instead, they're much simpler to configure and check and also more flexible (IMO).
deadbang

djk44883
Posts: 107
Joined: 2010-12-11 13:14
Has thanked: 2 times

Re: Cron.daily not running user scripts

#3 Post by djk44883 »

Who owns the file?
https://manpages.debian.org/buster/cron/cron.8.en.html
under Notes:
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root.
This may not be your problem.
I add myself to the crontab group (not sure if it's necessary) and create cron jobs in /etc/cron.d (owned by root) and for the user I run them as non-root, my own user name. I know each user has a crontab, but it's not like you can just click a file and edit it. This is unlikely ideal, but for my single user system it gets things done with little fuss.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#4 Post by Deb-fan »

Might help telling folks what you're trying to do. Surely not wanting to echo hello world to a file, so must be trying to learn how to use scripts. What Head_on said is a biggy, starting the script with a proper shebang is essential #!/bin/sh is no doubt linked to the system shell (in Debian that'll be dash), so if a script is using any bashisms would start it with #!/bin/bash so bash executes it and naming it with the .sh extension is good too.

Comes to executing a script, yep "crontab -e" as your user or as root adding -u yourusername /path/to/script/scriptname.sh

Another approach create an rc.local file in /etc and add what you want to it. As long as there's an rc.local file and it's executable "sudo chmod +x /etc/rc.local" the rc-local.service of systemd will run it. Finally I use a systemd service unit to run a script as follows. You create a service unit file in /etc/systemd/system I named mine startup.service here's the contents.

Code: Select all

[Unit]
Description=Running a script with systemd unit.

[Service]
Type=simple
ExecStart=/bin/bash /usr/local/bin/nameofscript.sh

[Install]
WantedBy=multi-user.target
I put the script named startup.sh in /usr/local/bin cause it's already in my path and chose multi-user.target which is equivalent to runlevel 3-4, you could change that to whatever if there's a more appropriate time (earlier/later) to run your script. After doing such stuff enable the service unit so it automatically starts at boot.

"sudo systemctl enable nameofservice.service" that's it puppy runs every boot. Oops, also you can check on your service with the "systemctl status nameofservice.service" command too if it succeeds it'll tell you plainly. Though time to check that is really before bothering to enable the service. Also just to be anal the service unit is systemctl's default type, so somebody doesn't have to type out the .service part in these systemctl cmds.
Last edited by Deb-fan on 2020-02-20 02:55, edited 4 times in total.
Most powerful FREE tech-support tool on the planet * HERE. *

djk44883
Posts: 107
Joined: 2010-12-11 13:14
Has thanked: 2 times

Re: Cron.daily not running user scripts

#5 Post by djk44883 »

Deb-fan wrote:"sudo systemctl enable nameofservice.service" that's it puppy runs every boot
This is really the "right" way to do thing (I suppose).
While we're on cron jobs, depending on the complexity or simplicity of task there's als0 @reboot in place of time and day specs in crontab.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#6 Post by Deb-fan »

That's how I was doing it, as root user "crontab -e". Scroll down to the obvious place and add

@reboot /path/to/scriptname.sh

Could also add the /bin/bash as shown in other post. Yes have it in path (at least use full path to it), yes start with proper shebang and name the file with the .sh extension, yes make it executable with chmod. :) Moved to an @reboot crontab cause rc.local is now frowned upon, then started using a systemd unit to get more familiar with systemd's use. All of them worked fine, though none 100% answer why cron.daily isn't for OP, would say it's due to what Head_on already pointed out, shebang is scripting 101.

PS, though if someone wants the finest grain control of timing or have their script run repeatedly at x-interval then clearly a crontab is the way to go or possibly setting up a systemd timer. I've yet to tackle that and don't even see a need/use for them since crons been around forever and is known to be rock solid for such a system function, shrugs. Kinda like systemd's dev team just had time on their hands.
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#7 Post by Head_on_a_Stick »

Deb-fan wrote:"sudo systemctl enable nameofservice.service" that's it puppy runs every boot.
The OP wants it to run every day rather than every boot, which is why I suggested a .timer unit rather than just a plain .service unit. See systemd.timer(5) for details.
deadbang

djk44883
Posts: 107
Joined: 2010-12-11 13:14
Has thanked: 2 times

Re: Cron.daily not running user scripts

#8 Post by djk44883 »

@daily
...if you have a functioning script - to get the "@" options on topic

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#9 Post by Deb-fan »

Surely a timer would work. Just resistant to bother learning about them with cron to handle it. This kind of thing is it's whole purpose for existing. Thing/cron is almost certainly running already on a system anyway so not really seeing a good reason not to use it and having to bother with adding some systemd unit to the mix. Overall just thinking of this as a tomato vs moto type of situation. Though of course agree @reboot and the service unit I mentioned don't really fit the bill. Only wanted to elaborate on them cause they're cool/useful and can be used for running of scripts. Great tyvm Head_on, now I'll very likely look into these damn systemd timer thingys! :(

Though personally been considering trying trimming down systemd so not overly excited with the prospect of expanding use of it. Out of curiosity why'd you advise use of a timer over a crontab anyway? Some advantage I'm not able to see here?
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#10 Post by Head_on_a_Stick »

Deb-fan wrote:why'd you advise use of a timer over a crontab anyway?
I'm not advising it, as such, that's just what I would use. And that's only because I've never tried to use cron jobs so they confuse me.
Deb-fan wrote:Some advantage I'm not able to see here?
How about

Code: Select all

empty@E485:~ $ systemctl list-timers --no-p
NEXT                         LEFT                LAST                         PASSED             UNIT                         ACTIVATES
Thu 2020-02-20 11:31:56 GMT  50min left          Thu 2020-02-20 10:32:17 GMT  8min ago           anacron.timer                anacron.service
Fri 2020-02-21 00:00:00 GMT  13h left            Thu 2020-02-20 08:25:24 GMT  2h 15min ago       logrotate.timer              logrotate.service
Fri 2020-02-21 00:00:00 GMT  13h left            Thu 2020-02-20 08:25:24 GMT  2h 15min ago       man-db.timer                 man-db.service
Fri 2020-02-21 08:40:35 GMT  21h left            Thu 2020-02-20 08:40:35 GMT  2h 0min ago        systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2020-02-24 00:00:00 GMT  3 days left         Mon 2020-02-17 05:09:10 GMT  3 days ago         fstrim.timer                 fstrim.service
Fri 2020-03-06 09:48:44 GMT  2 weeks 0 days left Sat 2020-02-01 10:19:30 GMT  2 weeks 5 days ago btrfs-scrub@-.timer          btrfs-scrub@-.service

6 timers listed.
Pass --all to see loaded but inactive timers, too.
empty@E485:~ $
Can you do that for cron jobs?
deadbang

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#11 Post by Deb-fan »

Kinda cool but you don't really need to, crontabs are set/forget affairs in my view. Though can list them if someone likes with "crontab -l". Show all those set for a user. Wonder about timing here, as to how systemd timers keep track? Supposedly cron mostly expects a system is up 24/7 but by default still checks for jobs every 60secs, so assuming any crontab set to run daily that hasn't been is going to get ran regardless. What about these timer majigs, how do they handle such in the event that the system is down?

Ps, my main point of contention for them, is what justifies any need with cron having been around forever to handle this exact thing and it's system overhead is tiny. Just seems painfully redundant. Could very well be missing something too but as things stand wondering why waste time integrating something like these timer deals, shrugs. Mostly like systemd personally but do agree it seems to shoot for continued expansion into weird areas. What's next, systemd-calendard, the devs are gobbling up some fairly oddball junk that seems out of place for anything a services/device manager need handle. :)
Most powerful FREE tech-support tool on the planet * HERE. *

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#12 Post by Deb-fan »

That above really is something that should be clarified with both these approaches for real. If someone sets either to run daily, 12:01 rolls around, system is down, thing still runs or no? Would have to test to know 100% but not that curious. Perhaps somebody more familiar with it will pipe up.
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#13 Post by Head_on_a_Stick »

Deb-fan wrote:If someone sets either to run daily, 12:01 rolls around, system is down, thing still runs or no?
It would run if the anacron package is installed, that's what it's for.

Now we should probably stop chatting and wait for the OP to reply.
deadbang

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#14 Post by Deb-fan »

Nah the OP's mystery was solved when ya pointed out starting a script with a shebang is required. :) Anacron might not be enabled on a system? Apparently there are different cron scheduling daemons (fcron) which will handle this type of situation but with the regular one, nope, system is off when x-job is set to run, it's skipped. This fcron thing runs missed jobs asap for folks. That's just going off a couple mins of googling. Still this is relevant and something folks need consider. Don't have any critical junk I absolutely have to have run daily but if were to this kind of thing could be very important.
Most powerful FREE tech-support tool on the planet * HERE. *

DocTomJP
Posts: 82
Joined: 2015-06-08 05:21
Location: Thailand
Been thanked: 1 time

Re: Cron.daily not running user scripts

#15 Post by DocTomJP »

Gentlemen, Many thanks!
Please understand that I am a novice in all this, and I am struggling to get to some small level of competency. I am attempting to write my first scripts and my first use of cron/anacron.
Some responses to your points....

1. The shebang is actually #!/bin/sh I am sorry that I did not copy it correctly when I posted, and I did not notice my error.
2. The owner is root, and the group is root.
3. I am using the HiWorld script as a test because I could not get any script to work. I was at first trying to get a simple script to run in cron.weekly, but everything failed so I started the test in cron.daily because of the greater frequency for testing.
4. The contents of cron.daily are
0anacron bsdmainutils HiWorld man-db
apt-compat cracklib-runtime locate passwd
aptitude dpkg logrotate popularity-contest
I am not sure what you want when you say that I should post the actual (ana)cron job.
5. As to the recommendations to use systemd to execute timed jobs, I think that I will have to do some reading before I go in that direction.

Many thanks indeed for all your attention to this. I feel sure that I have made a stupid mistake somewhere, (but not in the shebang.)
Tom.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#16 Post by Deb-fan »

No worries it's a never ending learning process, doesn't matter how long someone has used gnu/nix, will forever be things to learn and changes too. Been at it for almost 10yrs and still rightly consider myself a noob. Plenty of free PDFs online on the topic of scripting and or bash. Your os has more than one too a system shell (in Debian that's dash)and a user shell = bash. Both can be changed, reason I mention starting some scripts with #!/bin/bash is cause some things which work in bash won't in other shells aka: bashisms.

Starting it with #!/bin/sh is going to run it with the system shell likely. Check in terminal what that is with "ls -l /bin/sh". You'll see it's linked to -> dash. Without appending the cmd to run it with /bin/bash so bash does. Stuff specific to bash in a script won't or may not bother working. The other stuff just wanted to point it out cause it's obvious you're trying to learn about this type of thing. Crontabs can be set to run as frequently as you like, set them to. Not familiar with the systemd .timer units/things seems an either or depending on preference and/or exact purpose for what someone's doing. Both of them are well docced though. So you just need to hit up Google. Just a good general idea not to reinvent the wheel when possible. Folks have shared working script examples online. Grab some of them and study how they work to help learn, 2 cents.

10yrs in and my cron, scripting and bash skills still suck but I'm a helluva clog dancer, errrr or could be, honestly I don't know how to clog dance either. :P
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#17 Post by Head_on_a_Stick »

DocTomJP wrote:I am not sure what you want when you say that I should post the actual (ana)cron job.
The content of /etc/cdron.daily/HiWorld
deadbang

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Cron.daily not running user scripts

#18 Post by Deb-fan »

Reason for advocating crontabs is just they've been in use forever and should be better doc'ed with examples and what not. Assume by this point cron/tabs have to be as rock solid and versatile in terms of features as anything can get. The systemd deal is newish, though want to speculate probably utilizes cron itself? Head_on may know if such is the case? Dunno and time to shut it. Good luck with your scripting adventures. :) You'll get it with effort or if not just do what I do, still don't get it? Keep pm'lng Head_on and bug him until he explains it in terms us mere tech-mortals can understand.
Most powerful FREE tech-support tool on the planet * HERE. *

DocTomJP
Posts: 82
Joined: 2015-06-08 05:21
Location: Thailand
Been thanked: 1 time

Re: Cron.daily not running user scripts

#19 Post by DocTomJP »

Thanks again. Update - here is the content of /etc/cron.daily/HiWorld

Code: Select all

#!/bin/sh
set -e 
echo "hello world" > ~/Documents/TestHiW
I thought (think) this is exactly the same as the script that I posted previously, hence my confusion.
As I mentioned the script runs ok from the command line with sh HiWorld. But when I try to force the cron.daily to run I now get

Code: Select all

sudo run-parts /etc/cron.daily
/etc/cron.daily/HiWorld: 3: /etc/cron.daily/HiWorld: cannot create /root/Documents/TestHiW: Directory nonexistent
run-parts: /etc/cron.daily/HiWorld exited with return code 2
I thought thatI was directing the output to my home/Documents directory, That is what happens when I run it from the command line. But it seems that it wants to send to root. Perhaps I am making a wrong interpretation of the error.

Many thanks once again for your attention and patience.
Tom.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Cron.daily not running user scripts

#20 Post by Head_on_a_Stick »

DocTomJP wrote:I thought thatI was directing the output to my home/Documents directory, That is what happens when I run it from the command line. But it seems that it wants to send to root.
The cron job is run as root so the tilde ("~") in the script sends it to /root

Remove the tilde and use the full path instead.
deadbang

Post Reply