Shell Script Run by Cron That Has GUI and Sound Elements

samTopaz

New Member

Reaction score: 1
Messages: 15

Hello,

I've been working on a script to automatically put my computer to sleep when it gets below a certain battery percentage level. It uses zenity for dialog boxes and mpv for alert sounds. I designed it to be run as a Cron job. It seemed to be working when I tested it on the command line but when I tried to use it with Cron I realized that it may not be as easy as I had thought it would have been. Basically I want it to be run as a Cron job but it doesn't seem to work and I'm pretty sure it has something to do with my use of zenity and mpv.

Anyway this is the script: https://github.com/samcdc6600/sleep-FreeBSD-on-low-battery/blob/master/solp

Actually I think the sound might be working as I just heard it LOL. I've still got the entry I mad in my users crontab file with crontab -e. It is "*/4 * * * * export DISPLAY=:0 && /usr/bin/solp".

As you can see I've tried adding "DISPLAY=:0 &&" but I don't think it's working.

Any help would be much appreciated.
 
OP
samTopaz

samTopaz

New Member

Reaction score: 1
Messages: 15

Also I know I could change the script and have it running all the time in the background. But I sort of wanted to do it using Cron and although it may still have bugs I've been working on it all day trying to get it just right so i don't really feel like changing it a whole lot when it seems to work (with the obvious exception of the whole GUI thing.)
 

Elazar

Well-Known Member

Reaction score: 182
Messages: 455

Such things should be handled by implementing the _BTP method into the cmbat driver, really. ;D

You can add something like export > /tmp/export.txt to the cron script, just to see what environment variables are available.
 

ralphbsz

Son of Beastie

Reaction score: 1,985
Messages: 2,945

Why cron? Cron is designed for things that run on a fixed schedule, independent of what else is going on in the computer. The origin of cron was servers that were up 24x365. Using cron for things that depend on what is going on in the computer (like who is logged in, whether they are using a GUI or not, who is on the GUI if there is only one, ...) is sort of abusing cron.

I can express the same criticism another way: cron does not know who is logged in, and cron jobs are very independent. That's why they don't have any environment variables. You noticed that and added "DISPLAY=:0". Well, what if you're logged in tomorrow on display ":2.3"? What if you're logged in on display othermachine.mydomain.com:4.5? What if your friend Adam is logged in on :0? What if the person who is logged in only speaks French, and you display messages in English?

I think you need to redesign how this should work on a multi-user system (which is what most Unix-derived systems are). If it uses zenity, it should probably be tied into whatever is used to log in (like xdm), which is where it would gets its environment from.
 
OP
samTopaz

samTopaz

New Member

Reaction score: 1
Messages: 15

Why cron? Cron is designed for things that run on a fixed schedule, independent of what else is going on in the computer. The origin of cron was servers that were up 24x365. Using cron for things that depend on what is going on in the computer (like who is logged in, whether they are using a GUI or not, who is on the GUI if there is only one, ...) is sort of abusing cron.

I can express the same criticism another way: cron does not know who is logged in, and cron jobs are very independent. That's why they don't have any environment variables. You noticed that and added "DISPLAY=:0". Well, what if you're logged in tomorrow on display ":2.3"? What if you're logged in on display othermachine.mydomain.com:4.5? What if your friend Adam is logged in on :0? What if the person who is logged in only speaks French, and you display messages in English?

I think you need to redesign how this should work on a multi-user system (which is what most Unix-derived systems are). If it uses zenity, it should probably be tied into whatever is used to log in (like xdm), which is where it would gets its environment from.
Well I decided to use Cron because I want the script to run on a fixed schedule to check the battery status. So in terms of that aspect of the functionality I don't really see how it is an abuse of Crom. It was only later that I decided to add the Zenity and Mpv stuff. I can understand how that could be seen as an abuse of Cron and I think I will change it so that it is started with X and just runs all the time.
I find your critique of my messages only being in English a bit absurd however as you'll notice I said "put my computer to sleep". This script is for my personal use I see no reason why I should support multiple languages. Anyway with that being said I am greatfull to you for your reply and can see I should change my script a little :).
 
OP
samTopaz

samTopaz

New Member

Reaction score: 1
Messages: 15

Such things should be handled by implementing the _BTP method into the cmbat driver, really. ;D

You can add something like export > /tmp/export.txt to the cron script, just to see what environment variables are available.
I don't know what you mean with this _BTP combat stuff. I assume it's a joke or something :). Anyway thanks for your reply. I think I'll just change my script as I can see now that running stuff that needs a GUI from a Cron script probably isn't a good idea.
 

vigole

Daemon

Reaction score: 1,173
Messages: 1,093

I don't know what you mean with this _BTP combat stuff. I assume it's a joke or something
No, it's not a joke. this is part of ACPI specifications.

base/head/sys/contrib/dev/acpica/common/ahpredef.c

C:
const AH_PREDEFINED_NAME    AslPredefinedInfo[] =
{
    [...]
    AH_PREDEF ("_BTP",    "Battery Trip Point", "Sets a Control Method Battery trip point"),
    [...]
};
 

ralphbsz

Son of Beastie

Reaction score: 1,985
Messages: 2,945

Well I decided to use Cron because I want the script to run on a fixed schedule to check the battery status.
Even if nobody is logged in on the console? Even if someone is logged in, but not using X and a GUI? If someone else is logged in? Now, you might respond: these are not use cases that you expect to be important. In that case, carry on.

I find your critique of my messages only being in English a bit absurd however as you'll notice I said "put my computer to sleep". This script is for my personal use I see no reason why I should support multiple languages.
Touche, criticism accepted. I do the same thing, I write programs for my own use only in a single language, and usually with very brief messages. One bad habit I used to have: using random insults instead of error messages. So where a normal program would print something like "12 is not a valid input" or "your disk drive is offline", I would print "your face looks like a pizza" or "your feet smell like anchovies". That's because if I'm the only user of a program I wrote, I will look in the source code whenever something goes wrong, so just printing a key word like "pizza" is good enough to find the error message.
 

Elazar

Well-Known Member

Reaction score: 182
Messages: 455

I don't know what you mean with this _BTP combat stuff. I assume it's a joke or something :). Anyway thanks for your reply. I think I'll just change my script as I can see now that running stuff that needs a GUI from a Cron script probably isn't a good idea.
It was a rant. I once started a patch for the cmbat driver, but never finished it. It would create a sysctl, that would trigger a devd event on a certain battery level.
Code:
nioh@nioh:~ % sysctl dev.battery.0.Warning_Level
dev.battery.0.Warning_Level: 20

The idea is that you would avoid the need for a daemon (e.g. cron) that constantly checks battery levels. I will look into it again.
 
OP
samTopaz

samTopaz

New Member

Reaction score: 1
Messages: 15

No, it's not a joke. this is part of ACPI specifications.

base/head/sys/contrib/dev/acpica/common/ahpredef.c

C:
const AH_PREDEFINED_NAME    AslPredefinedInfo[] =
{
    [...]
    AH_PREDEF ("_BTP",    "Battery Trip Point", "Sets a Control Method Battery trip point"),
    [...]
};
haha okay thanks :). Yeah I don't really know much about ACPI stuff.
 

monwarez

Member

Reaction score: 34
Messages: 84

I use cron to fetch my mail and then display notification, to do that I have to export the XAUTHORITY and DISPLAY used by my user. I use a shell scripts for that. If you need to access dbus you can export it too. Of course this is in a single X11 user design
Code:
#!/bin/sh
XAUTHORITY=$HOME/.Xauthority
export XAUTHORITY
DISPLAY=:0
export DISPLAY

dbus_session_file=$HOME/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0

if [ -e "$dbus_session_file" ]; then
    . "$dbus_session_file"
    export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
    runMyProgramThatNeedAccessToDbus
fi
 

dave01

Member

Reaction score: 7
Messages: 22

Apart from what others have already said, don't forget that running a script as you in a shell is different to cron running the script. Different environment and, most commonly a cause of problems, different path environment variable so executeables and/or other required files may not be found.
 
OP
samTopaz

samTopaz

New Member

Reaction score: 1
Messages: 15

Apart from what others have already said, don't forget that running a script as you in a shell is different to cron running the script. Different environment and, most commonly a cause of problems, different path environment variable so executeables and/or other required files may not be found.
Yeah I've changed it so that it runs forever and I'm starting it after I start X. Seems to be working alright.
 
Top