You are not logged in.

#1 2012-07-29 21:57:39

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

[SOLVED] Bash script doesn't work when run by crond

I recently started experiencing crashes, and since I suspect they're heat related, I wrote the following script to monitor hw temperatures, but for some reason the script never writes the result of

nvidia-settings -q gpucoretemp -t

when crond runs it. It works perfectly fine when I run it, and when I run it root though. I've no idea what is going on?

#!/bin/bash
 FILE=/home/bladt/core/gputemp
 DIR=$(dirname $FILE)
 ENTRY_SEP=###########################ENTRY#############################
  
  cd $DIR
  cpu=$(sensors)
  gpu=$(/usr/bin/nvidia-settings -q gpucoretemp -t)
  time=$(date '+%D %T')
  echo -e "$ENTRY_SEP \n$time \n$cpu \nnvidia gfx: $gpu \n" >> $FILE
  
  cat $FILE | tail -n1080 > tmp
  mv tmp $FILE

What am I missing here?

Last edited by Bladtman242 (2012-07-29 23:33:08)

Offline

#2 2012-07-29 22:15:09

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] Bash script doesn't work when run by crond

Presumably, nvidia-settings wants access to your X session, which it doesn't have.

Offline

#3 2012-07-29 22:16:47

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED] Bash script doesn't work when run by crond

I suppose that does make sense.
is there anything I can do about it?

Offline

#4 2012-07-29 22:20:38

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,152

Re: [SOLVED] Bash script doesn't work when run by crond

Wouldn't that mean that root would have trouble, too? (Or did you mean you ran it with sudo?)

I don't know anything about nvidia but perhaps the following might be at least indirectly helpful:

# ref.: http://unix.stackexchange.com/questions/28463/run-a-dbus-program-in-crontab-how-to-know-about-the-session-id

ksession_pid=$(ps -o pid= -C 'kwin -session' | sed 's/ //g')

if [ "$ksession_pid" = "" ]
then
        dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
        if [ -e "$dbus_session_file" ]
        then
                . "$dbus_session_file"
                [ $? == 0 ] || ((allan++))
                export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
        else
                error "Gwall! Allwn i ddim canfod $dbus_session_file."
        fi
else
        dbus_session_address=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$ksession_pid/environ | sed 's/^DBUS_SESSION_BUS_ADDRESS=//')
        [ "$dbus_session_address" != "" ] || error "Could not get DBUS_SESSION_BUS_ADDRESS for session." 
        export DBUS_SESSION_BUS_ADDRESS="$dbus_session_address"
fi

Last edited by cfr (2012-07-29 22:26:06)


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#5 2012-07-29 22:25:38

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED] Bash script doesn't work when run by crond

I meant I ran it with sudo.
Now that I tried running it as root, I get the following:

ERROR: The control display is undefined; please run
`/usr/bin/nvidia-settings --help` for usage information.

Offline

#6 2012-07-29 22:40:18

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,646

Re: [SOLVED] Bash script doesn't work when run by crond

what does the crontab look like? Here is one I have that runs a script to change the wallpaper every ten minutes:

*/10 * * * * DISPLAY=:0.0 /home/dogs/.scripts/random-wallpaper

without the DISPLAY=:0.0 the script ran, but could not change the wallpaper.

Last edited by 2ManyDogs (2012-07-29 22:41:42)

Offline

#7 2012-07-29 22:42:48

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED] Bash script doesn't work when run by crond

It looks like this:

# minute        hour    Day of Month    Month   Day of Week     Command
# (0-59)        (0-23)  (1.31)          (1-12)  (0-6)

0-59/2          *       *               *       *               /home/bladt/prompt/batlife
0-59/2          *       *               *       *               /home/bladt/core/core

I tried using the --display flag and the --config flag (separately) with nvidia-settings, but no luck smile

Last edited by Bladtman242 (2012-07-29 22:44:35)

Offline

#8 2012-07-29 22:47:13

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,646

Re: [SOLVED] Bash script doesn't work when run by crond

Bladtman242 wrote:

I tried using the --display flag and the --config flag (separately) with nvidia-settings, but no luck smile

I don't understand this. Did you make a crontab entry with the DISPLAY=:0.0 on it, or just call nvidia-settings with a --display option in the script?

Offline

#9 2012-07-29 23:31:14

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED] Bash script doesn't work when run by crond

2ManyDogs: I just called nvidia-settings with the flag, as root.
cfr: Unfortunately, the script ends in the "error "gwall.." branch for me.

EDIT: Aaand success!
Seting DISPLAY in the crontab did the trick.
Thank you all smile
Not sure I understand why it didn't help as flag for nvidia-settings then, but what the heck smile

Last edited by Bladtman242 (2012-07-29 23:32:41)

Offline

#10 2012-07-29 23:57:39

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,646

Re: [SOLVED] Bash script doesn't work when run by crond

Glad you got it working. It didn't work with the nvdia flag because jobs run by cron don't have access to X unless you explicitly specify the display in the crontab. That's what falconindy was trying to tell you wink

Last edited by 2ManyDogs (2012-07-29 23:58:06)

Offline

#11 2012-07-30 15:52:37

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED] Bash script doesn't work when run by crond

Huh, it just beats me how setting the exact same variable just before running the command in cron makes a difference?

Offline

Board footer

Powered by FluxBB