sceamindynomit
Posts: 16
Joined: Fri Jun 26, 2015 12:08 am

bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 3:09 am

ok heres the scripts i cp -r into /etc/cron.hourly

#/bin/bash

## Settings - Put your directory to your wallpaper folder here.. (example: /home/pi/Pictures/Wallpapers)
dir='/home/pi/Downloads/walls'

## select a random image
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"`

## pushing result to pcmanfm (Which manages the wallpaper)
pcmanfm --set-wallpaper="$path"

## Step Three - exit
exit 0

i can execute it manualy but it wont run thru cron

i did sudo chmod +x /home/pi/Desktop/desktop/wall

then sudo cp -r /home/pi/Desktop/desktop/wall /etc/cron.hourly
any ideas why it wont work?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 3:15 pm

missing "!" in first line, it should be

Code: Select all

#!/usr/bin
Probably better to install the job in the users CRON list using "crontab -e" rather than make it a system job.

Not sure if the pacmanfm command needs $DISPLAY to be set.
try

Code: Select all

env -i ./the_name_of_the_script
if it still works then there should be no problem.

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 3:42 pm

sprinkmeier wrote:missing "!" in first line, it should be

Code: Select all

#!/usr/bin
For a script it should be the path to an appropriate shell, for example

Code: Select all

#!/usr/bin/bash
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 3:49 pm

PeterO wrote:
sprinkmeier wrote:missing "!" in first line, it should be

Code: Select all

#!/usr/bin
For a script it should be the path to an appropriate shell, for example

Code: Select all

#!/usr/bin/bash
Exactly!

In this case it should be

Code: Select all

#!/bin/bash
;)

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 4:02 pm

My excuse is I'm sitting in front of a Windows machine right now ! :( :roll:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 4:05 pm

That sounds like some form of torture to me :( :lol:

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 6:43 pm

rpdom wrote:That sounds like some form of torture to me :( :lol:
Corporate IT system, so yes, torture !
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: bash script wont auto-run from cron.hourly

Fri Jun 26, 2015 10:31 pm

PeterO wrote:My excuse is I'm sitting in front of a Windows machine right now ! :( :roll:
PeterO
I'm not sure I've got one :-) (other than it being late at night)
That sounds like some form of torture to me :( :lol:
I feel that way about Windows too!

sceamindynomit
Posts: 16
Joined: Fri Jun 26, 2015 12:08 am

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 12:38 am

ok crontab dosnt seem to be working i put

1-24 * * * /home/pi/Desktop/wall

am i useing it right to have it change hourly?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 1:28 am

sceamindynomit wrote:ok crontab dosnt seem to be working i put

1-24 * * * /home/pi/Desktop/wall

am i useing it right to have it change hourly?
It'll change every minute for the first 24 minutes of every hour :-)

try

Code: Select all

@hourly  /home/pi/Desktop/wall

sceamindynomit
Posts: 16
Joined: Fri Jun 26, 2015 12:08 am

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 2:41 am

it dosnt seem to be even trying to run the cronjob any ideqas? do i have to start crontab as a process? if so how would i do that?(im a semi noob with linux)

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 7:32 am

As sprinkmeier said, you've set it to run at minutes 1 to 24 past each hour. If you want it to run every hour you could do

Code: Select all

0 0-23 * * /home/pi/Desktop/wall
(There is no "24" on a 24 hour clock. It runs from 00:00 to 23:59)
or

Code: Select all

0 * * * /home/pi/Desktop/wall
The first method is slightly clearer, but you can use either.

To check that it is being started, wait until just after the hour and then

Code: Select all

rpdom@raspi5 ~ $ tail /var/log/auth.log
Jun 27 08:20:01 raspi5 CRON[31559]: pam_unix(cron:session): session opened for user rpdom by (uid=0)
Jun 27 08:20:03 raspi5 CRON[31559]: pam_unix(cron:session): session closed for user rpdom
Jun 27 08:25:01 raspi5 CRON[31589]: pam_unix(cron:session): session opened for user rpdom by (uid=0)
Jun 27 08:25:02 raspi5 CRON[31589]: pam_unix(cron:session): session closed for user rpdom
Jun 27 08:27:51 raspi5 sshd[31620]: Accepted publickey for rpdom from 192.168.1.42 port 41103 ssh2
Jun 27 08:27:51 raspi5 sshd[31620]: pam_unix(sshd:session): session opened for user rpdom by (uid=0)
There you can see CRON entries to show that something was run at that time.

To debug the cron script itself you can add logging to the entry

Code: Select all

0 0-23 * * /home/pi/Desktop/wall >/tmp/wall.out 2>/tmp/wall.err
When the cron runs it will create two files under /tmp. wall.out will contain any ordinary output from the run, and wall.err will contain any error messages. Because I suggested ">" instead of ">>" the files will get overwritten every time the cron runs, so only the latest ones will be there.

sceamindynomit
Posts: 16
Joined: Fri Jun 26, 2015 12:08 am

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 8:14 am

ok i figured out a way to avoid the annoyance of crontab heres the code

#/bin/bash
while [ true ]
do

## Settings - Put your dir to your wallpaper folder here.. (like: /home/pi/Pictures/Wallpapers)
dir='/home/pi/Downloads/walls'

## Step one - Random File selection
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"`

## Step two - pushing result to pcmanfm (Which manages the wallpaper ^^)
pcmanfm --set-wallpaper="$path"

## Step Three - exit
sleep 3600
done

i would have had to modify the script and there would have been layering violations it would have been a mess this is a simple yet elegant solution

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash script wont auto-run from cron.hourly

Sat Jun 27, 2015 8:23 am

sceamindynomit wrote:this is a simple yet elegant solution
No, it's a kludge. You now have no way of knowing when the next event will be !

Cron is exactly the thing you should be using. Did you try doing it properly with "crontab -e" ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

sceamindynomit
Posts: 16
Joined: Fri Jun 26, 2015 12:08 am

Re: bash script wont auto-run from cron.hourly

Sun Jun 28, 2015 12:54 am

yes i tryed 0 0-24 * * ect/ect/ect dosnt work but the sleep 3200 makes it sleep for 3200 seconds (or 1 hour so it will execute evey hour after the script is executed, which is all i was realy after but if t can be modified to work with crontabs i will try it) i dont think crontab works for shell scripts or at all even lol

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: bash script wont auto-run from cron.hourly

Sun Jun 28, 2015 3:43 am

sceamindynomit wrote:yes i tryed 0 0-24 * * ect/ect/ect dosnt work but the sleep 3200 makes it sleep for 3200 seconds (or 1 hour so it will execute evey hour after the script is executed, which is all i was realy after but if t can be modified to work with crontabs i will try it) i dont think crontab works for shell scripts or at all even lol
3200 seconds, is that an imperial hour?
I've been cron-ing scripts for decades, so if cron really doesn't support scripts I've been doing it wrong :-)

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: bash script wont auto-run from cron.hourly

Sun Jun 28, 2015 7:17 am

sprinkmeier wrote: 3200 seconds, is that an imperial hour?
Is that in the same way that 16oz is an American pint.

60*60 == 3600 in my mathematical system.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Troubleshooting”