0

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?

  • 2
    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. – Rinzwind Aug 23 '21 at 15:31
  • @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. – Rogerio Schmitt Aug 23 '21 at 15:43
  • 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). – Markus Ueberall Aug 23 '21 at 15:45
  • @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 Aug 23 '21 at 16:05
  • 1
    @MarkusUeberall by the way, `poweroff` (/usr/sbin/poweroff) liks to /bin/systemctl wgich can be executed by anyone so `sudo` isn't needed anyway. – Soren A Aug 23 '21 at 16:11
  • @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. – Markus Ueberall Aug 23 '21 at 16:23
  • @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. – Soren A Aug 23 '21 at 16:46
  • @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). – Markus Ueberall Aug 24 '21 at 07:07
  • 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. – James S. Aug 25 '21 at 14:42

1 Answers1

1

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