Score:4

Crontab never works on ubuntu server

vu flag

I'm really new to ubuntu server, and i've been having a lot of issues with crontab across this week and i can't really figure out what's wrong with it, i tried everything i saw here to no avail.

So, i have a script that in theory should shut off the server after a few minutes of net inactivity. The script executes without any problem, and works perfectly if done so manually. The issue i've been facing happens when I try to setup cron to execute this script.

Here's the code

#!/bin/bash 
set -o nounset -o errexit -o pipefail

MARKER_FILE="/tmp/ssh-inactivity-flag"

STATUS=$(netstat | grep ssh | grep ESTABLISHED &>/dev/null && echo active || echo inactive)   

if [ "$STATUS" == "inactive" ]; then
  if [ -f "$MARKER_FILE" ]; then
    echo "Powering off due to ssh inactivity."
    rm --force "$MARKER_FILE"
    /sbin/poweroff  # See https://unix.stackexchange.com/a/196014/56711
  else
    # Create a marker file so that it will shut down if still inactive on the next time this script runs.
    echo "El cabron aun no se ha ido :("
    touch "$MARKER_FILE"
  fi
else
  # Delete marker file if it exists
  rm --force "$MARKER_FILE"
  echo "No se va el pussi"
fi

Here's the crontab setting:

*/1 *   * * *   root    /home/angel/idle.sh

And here's what it returns on /var/log/syslog

Jun 29 13:37:01 torreon CRON[1951]: (angel) CMD (root    /home/angel/idle.sh)

In theory cron is executing the script, but it doesn't seem to work or return anything. I'm really lost and i don't know what i could have done wrong, so if anyone can please guide me it would be greatly appreciated.

(Also i've tried with other scripts that are just like one line of code and those didn't work neither.)

Raffa avatar
jp flag
`CMD (root /home/angel/idle.sh)` shows that cron recognizes `root` as a part of the command ... Specifying a user is not supported here in a user crontab, so remove the user part i.e. `root` ... If you need to execute your script with elevated privileges, then add it with `sudo crontab -e`
Ángel avatar
vu flag
Crap, removing that and executing crontab with sudo seemed to fix it. Sorry for asking such dumb question, thx you so much.
Raffa avatar
jp flag
Don't be sorry ... That is a valid and smart question because it saved your time and will save a lot of other users time when they search for the same issue and find this post ... You are welcome.
hr flag
Somewhat related: [cron jobs - difference between editing "/etc/crontab" and "crontab -e"](https://askubuntu.com/questions/900876/cron-jobs-difference-between-editing-etc-crontab-and-crontab-e)
Score:7
jp flag

CMD (root /home/angel/idle.sh) shows that CRON recognizes root as a part of the command and tries to execute it which, normally, will fail ... Specifying a user is not supported here in a user crontab, so remove the user part i.e. root.

The command crontab -e(without sudo) edits the invoking user's crontab.

If you need to execute your script with elevated privileges, then add it with sudo crontab -e which will edit the root’s user crontab.

From man crontab:

Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.

and:

The following lists the content of a regular system-wide crontab file. Unlike a user's crontab, this file has the username field, as used by /etc/crontab.

I sit in a Tesla and translated this thread with Ai:

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.