Cron.daily not running user scripts [SOLVED]

If none of the more specific forums is the right place to ask

Cron.daily not running user scripts [SOLVED]

Postby DocTomJP » 2020-02-18 08:09

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.
DocTomJP
 
Posts: 28
Joined: 2015-06-08 05:21

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-18 09:13

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).
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Cron.daily not running user scripts

Postby djk44883 » 2020-02-19 21:43

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.
djk44883
 
Posts: 89
Joined: 2010-12-11 13:14

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-19 22:40

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.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby djk44883 » 2020-02-19 23:17

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.
djk44883
 
Posts: 89
Joined: 2010-12-11 13:14

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-19 23:32

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.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-20 09:05

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.
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Cron.daily not running user scripts

Postby djk44883 » 2020-02-20 09:18

@daily
...if you have a functioning script - to get the "@" options on topic
djk44883
 
Posts: 89
Joined: 2010-12-11 13:14

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-20 09:36

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?
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-20 09:53

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?
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-20 09:58

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. :)
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-20 10:32

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.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-20 10:39

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.
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-20 10:53

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.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby DocTomJP » 2020-02-21 02:48

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.
DocTomJP
 
Posts: 28
Joined: 2015-06-08 05:21

Next

Return to General Questions

Who is online

Users browsing this forum: No registered users and 13 guests

fashionable