Cron.daily not running user scripts [SOLVED]

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

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-21 03:12

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

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-21 04:42

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

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-21 06:02

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

Re: Cron.daily not running user scripts

Postby DocTomJP » 2020-02-22 04:08

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

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-22 11:54

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

Re: Cron.daily not running user scripts

Postby djk44883 » 2020-02-22 15:03

DocTomJP wrote: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


To elaborate on the answer to fix your issue, the environment you have on your command line... isn't the same environment user root has running a cron job. Good practice to use full path to files in cron.

You might also find some info about what happens, maybe, in /var/log/cron.log (or syslog depending on your /etc/rsyslog.conf configuration)

Although you may not specifically be using bash, if you want to get confused about ~ see this: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
djk44883
 
Posts: 90
Joined: 2010-12-11 13:14

Re: Cron.daily not running user scripts

Postby DocTomJP » 2020-02-23 05:52

Gentlemen, after your last advice, I did change to the full path,and it worked! Success!

I am nor sure that I understand why previously it was seeking to place the script in root, even after reading the article about tilde expansion. No matter, I can be pragmatic.

Now I will try to get my first real job running.

I now want to remove this HiWorld from cron.daily (and only that job) - please advise : do I just go into cron.daily and use the "rm" command on HiWorld?

I am extremely grateful to all the contributors on this issue. Head_on_a_Stick you have been very patient and helpful, so many thanks.
Tom.
DocTomJP
 
Posts: 28
Joined: 2015-06-08 05:21

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-23 07:37

Like Head_on and other person said, typing this in terminal (shells)and using it in scripts and stuff works as follows.

This ~/ is the same as this /home/username when you're logged in as root user or running something as root, the root user acct has it's own /home directory. So what you were doing was sending hi world to /home/root/Documents not your normal users Documents directory. Yep no worries this type of thing is confusing for awhile and still get confused often. :) Might want to take a minute and google about navigating in the gnu/Linux file system while using command-line (in terminal.)

PS, commands like cd = change directory and pwd = print working directory (the place in the filesystem you're currently in, in that terminal.) These are system admin 101 things. Ever want to become a nix guru, got to start with those 101 thingys. :)
Deb-fan
 
Posts: 708
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Deb-fan » 2020-02-23 08:00

Just being anal but while recently engaging in trying to figure out why Fluxbox wouldn't run it's startup file to launch apps. Was considering setting up a systemd service to do it and while at it noticed there is a systemd .timer thingy setup for cron.daily already. Which just pointing out but think it's odd, crons (daemon's) already installed and usually running on so many gnu/Linux os's out-of-box but using a systemd timer for it vs cron to handle errrr cron.daily? Errrr. :). Ah just noting it, this is something for geekier geeks than myself to geek out on.

Edit: Though am considering getting a pocket protector, just a practical thing and might start having stuff to write with/on more often.

Notices systemd lurking in the shadows, menacingly eyeing cron:

That's right cron, ya old booger, whose your daddy, ya saw what I did to sysV, may as well start shopping around for your casket. YOU'RE NEXT! MWAHHAHAHA! :)
Deb-fan
 
Posts: 708
Joined: 2012-08-14 12:27

Re: Cron.daily not running user scripts

Postby Head_on_a_Stick » 2020-02-23 11:37

DocTomJP wrote:I am nor sure that I understand why previously it was seeking to place the script in root, even after reading the article about tilde expansion.

Perhaps some examples will help.

Run this command as your normal user:
Code: Select all
echo ~

Now log in as root by running either
Code: Select all
sudo -i

Or
Code: Select all
su -

And then run the first command again.

Deb-fan wrote:So what you were doing was sending hi world to /home/root/Documents

Root's home directory is located at /root, try my examples above to confirm this.
User avatar
Head_on_a_Stick
 
Posts: 11964
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Cron.daily not running user scripts

Postby djk44883 » 2020-02-23 21:17

Deb-fan wrote:This ~/ is the same as this /home/username when you're logged in as root user or running something as root, the root user acct has it's own /home directory. So what you were doing was sending hi world to /home/root/Documents not your normal users Documents directory. Yep no worries this type of thing is confusing for awhile and still get confused often. :) Might want to take a minute and google about navigating in the gnu/Linux file system while using command-line (in terminal.)

Kind'a sort'a... builtin variables and xdg directores -- root has $HOME/Documents pointing to /root/Documents user root has it's "home" in /. This, as I understand, if /home is on another partition and system recover/maintenance mounting only the root system, well then user root has it's "home". So for user root ~/Documents expands to /root/Documents.

just say'n :)
djk44883
 
Posts: 90
Joined: 2010-12-11 13:14

Re: Cron.daily not running user scripts

Postby DocTomJP » 2020-03-05 07:17

Gentlemen : all is well. You have lead me to a solution. I am grateful indeed to you all; not only do I have a solution, but I learned a lot on the way, so many thanks indeed.
I will mark this topic as solved.
Tom.
DocTomJP
 
Posts: 28
Joined: 2015-06-08 05:21

Previous

Return to General Questions

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable