Score:0

Cron job to power off when AC is unplugged not running

in flag

I'm trying to run a simple bash script that turns off my notebook if it is not pluged on AC with a cron job.

My script is as follows:

#!/bin/bash

if ! on_ac_power; then 
    poweroff          
fi

And I've configured cron to run it every minute like so:

*/1 * * * * /home/user/Documents/script.sh

The script works just fine if I run it manually, but otherwise, it seems to have no effect under cron.

What am I missing here?

cn flag
Cron is not the correct tool for this. What you want is a service. what is `on_ac_power`? it gives nothing when on ac and when not on ac so seems useless for this. And poweroff I would add a directory to.
in flag
@Rinzwind `on_ac_power` tests whether the computer is running on line power. Add a directory do poweroff, how so? As I said the script works just fine if I run it manually, but cron seems to not be running it.
us flag
Apart from the previous comment, `poweroff` should require root privileges; `sudo poweroff` might work if the user in question is not required to enter a password. Better yet, put the cron entry into `/etc/crontab` and specify that it's to be executed as `root` (of course, the script should be moved to a sensible location as well in that case).
Soren A avatar
mx flag
@MarkusUeberall never use `sudo` in scripys and never ever in thingd that have ti be run in cron !! Instead run the script in root's cron if needed.
Soren A avatar
mx flag
@MarkusUeberall by the way, `poweroff` (/usr/sbin/poweroff) liks to /bin/systemctl wgich can be executed by anyone so `sudo` isn't needed anyway.
us flag
@Soren A: `/bin/systemctl` _can_ be executed by anyone, but executing `poweroff` as a non-root user will not necessarily work if other users are logged in (see `poweroff -i`, which I actually forgot to mention above); `sudo poweroff` _always_ works immediately.
Soren A avatar
mx flag
@MarkusUeberall .. but `sudo` doesn't worke in crontab ... the script must be run in root's cron. And for logged in users, there are seldom more than one user logged in on home systems . but off course a point to be aware of on larger systems.
us flag
@soren A `sudo` _does_ work in crontab as long as you don't need to enter a password (see e.g. https://askubuntu.com/questions/796617/how-to-avoid-password-request-for-sudo-for-crontab-scripts); whether it's a good idea to use it for the OP's use case is another question (see previous comments pointing to a service and `/etc/crontab` addressing this).
James S. avatar
de flag
Please clarify, in your question above, the full command you execute when you run the command manually, as well as output from ls showing the filesystem permissions of the script.
Score:1
in flag

First of all I was not running my cron under root.

Apparently for a cron to run under root you have to add it with sudo crontab -e, anything added just with crontab -e will not run as sudo.

Second as pointed in the reference:

cron runs in a very limited environment by default so a lot of commands that run via command name from a users terminal need to the full path in a crontab or a declaration at the beginning of the crontab to expand the path.

So I ended up replacing poweroff by /sbin/shutdown in my script, and now it runs as expected.

#!/bin/bash

if ! on_ac_power; 
then 
    /sbin/shutdown        
fi

Reference

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.