Solved Cron giving me fits

Solved issue

truckerDave

Active Member
Joined
Oct 7, 2023
Messages
250
Reaction score
234
Credits
2,111
My solution on page 2.

I have a directory /home/dave/swap/bgimages/ where I have many background images I have been pillaging from the internet. In that directory I created a bash script to change my background. Believe it or not, It wasn't too bad for someone of my limited Linux experience to figure out.

I can run the script while in the directory with bash background.sh and from the lowest directory with bash /home/dave/swap/bgimages/background.sh It works as intended with either one.

That said, I can't seem to get it to work using cron. I enter crontab -e and enter:

*/5 * * * * /home/dave/swap/bgimages/background.sh (should change every 5 minutes, right?)

It does absolutely nothing. What I have done to remedy this .... Set permissions for all files involved to 777 .... Chowned the same files dave:dave

How do I get this thing working. I know it's something staring me right in the kisser. But I'm not seeing it.

Thanks for your time.

EDIT: I also ran sudo crontab -u dave -l to verify it was under my account.
 
Last edited:


I can run the script while in the directory with bash background.sh
*/5 * * * * /home/dave/swap/bgimages/background.sh
When you run the script manually (and it works), you are using 'bash' + script. But in your cronjob, you aren't doing it that way.

I have a cronjob that runs like your cron example... without 'bash' being invoked... and it works. But I do have a bash shebang (#!/usr/bin/env bash) in the script. Do you have that? Just guessing. You may also need to add your script location in your $PATH.

Yes, */5 in the first column should run the cronjob every 5 minutes.
 
No. I don't have that exact code. Just what I've seen in my research #!bin/bash

(I've also changed some things like directory name to make it easier for me to remember what way is up.)

I know it may look a little weird. But I have a series of 288 images in that folder taken from a hi-res 24 hour timelapse video named like HH:MM.png. This calls for the png associated with the current time. I did this for the experience more than anything.

Code:
#!/bin/bash

min=$(echo "$(date "+%M") - ($(date +%M)%5)" | bc)
hour=$(echo "$(date "+%H")" | bc)
printf -v min "%02d" $min
printf -v hour "%02d" $hour

name=$(echo $hour":"$min".png")
dir="file:///home/dave/Dubai/new/"
#echo $name
#echo $dir$name
gsettings set org.gnome.desktop.background picture-uri $dir$name
 
Last edited:
#!/bin/bash should work fine too, almost always. I think there are some odd cases where bash is not in /bin (or some other trouble), but I've never encountered a problem with it myself. #!/usr/bin/env bash seems to be the more recommended way these days, that's all.

I'm not good enough with bash to critique your script or to guess much further. Your trouble may be in the code itself since you opened up all permissions already. Cron calls my script exactly like you do, but my script is only one line, and it works as expected.

@JasKinasis will be along here shortly since I'm calling his name, and I'm sure he'll have an idea or two to help you along. Good luck!
 
#!/bin/bash should work fine too, almost always. I think there are some odd cases where bash is not in /bin (or some other trouble), but I've never encountered a problem with it myself. #!/usr/bin/env bash seems to be the more recommended way these days, that's all.

I'm not good enough with bash to critique your script or to guess much further. Your trouble may be in the code itself since you opened up all permissions already. Cron calls my script exactly like you do, but my script is only one line, and it works as expected.

@JasKinasis will be along here shortly since I'm calling his name, and I'm sure he'll have an idea or two to help you along. Good luck!
I sure do appreciate your time @atanere.
 
Sorry I can't help more. Scripting is a challenge for me too.

Shell Check indicates some things in your code to take a look at... if you want to start there.
 
No. I don't have that exact code. Just what I've seen in my research #!bin/bash

(I've also changed some things like directory name to make it easier for me to remember what way is up.)

I know it may look a little weird. But I have a series of 288 images in that folder taken from a hi-res 24 hour timelapse video named like HH:MM.png. This calls for the png associated with the current time. I did this for the experience more than anything.

Code:
#!/bin/bash

min=$(echo "$(date "+%M") - ($(date +%M)%5)" | bc)
hour=$(echo "$(date "+%H")" | bc)
printf -v min "%02d" $min
printf -v hour "%02d" $hour

name=$(echo $hour":"$min".png")
dir="file:///home/dave/Dubai/new/"
#echo $name
#echo $dir$name
gsettings set org.gnome.desktop.background picture-uri $dir$name
your script looks fine to me. if you run the script manually I assume it works? if it does then try installing webmin on the system. It has a chron area with GUI for setting up the jobs and never had it fail as long as the script worked.
 
maybe use the absolut path to gsettings (try which gsettings)
 
maybe use the absolut path to gsettings (try which gsettings)
I was unaware that you could use vodka to get to gsettings. I will go purchase a bottle to help.

actually gsettings does not need absolute path. It works from whereever in my experience
 
xD lol typo.. absolute x)

cron might not have the same env as your shell. "give it a shot" ;)
 
xD lol typo.. absolute x)

cron might not have the same env as your shell. "give it a shot" ;)
which gsettings returned /usr/bin/gsettings

Added that into my script. Still no luck. Doesn't make much sense to me.
 
which gsettings returned /usr/bin/gsettings

Added that into my script. Still no luck. Doesn't make much sense to me.
does the script work if you are on the command line and just execute it manually?
 
your script looks fine to me. if you run the script manually I assume it works? if it does then try installing webmin on the system. It has a chron area with GUI for setting up the jobs and never had it fail as long as the script worked.
If all else fails, I may do that. It's not really all that important I get this running. I know the code works and that was my goal. To learn about something I know little of. I plan to rework the code and learn more about the proper syntax, etc.
 
try the webmin package to do the chron jobs. and keep in mind that you must use the absolute path in the chron entry showing exactly where the script is or it will not run. example... i have a script to print... it is called prt.sh to call it I would use sh /home/sadams/prt.sh

the sh in the beginning tells my fedora system it is a bash, then the space and full location of the file with name.
 
try the webmin package to do the chron jobs. and keep in mind that you must use the absolute path in the chron entry showing exactly where the script is or it will not run. example... i have a script to print... it is called prt.sh to call it I would use sh /home/sadams/prt.sh

the sh in the beginning tells my fedora system it is a bash, then the space and full location of the file with name.
I have the full path in there already. But I don't have that leading sh. I'll give that a shot. Won't hurt.
 
Does cron run by the clock time? Or from start time? I've not read anything stating one way or the other. But I'm thinking clock time. Right?
 

Members online


Top