Problem getting crontab to work

losarbitan

New Member
Nov 23, 2023
17
0
1
Hello all,

I am very new to proxmox, but have a couple years experience admining my homelab linux server.
I added a cronjob to crontab with crontab -e.
I want to send Analogj/Scrutiny metrics to another server every minute. This is my crontab:
Code:
@reboot (sleep 60 && echo "powersave" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)

# Run scrutiny metrics collector and export to Sapphire
# Every minute
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml

Usually the crontab contains some commented out stuff with information about how to use it, I did not see it in there. Not sure if that's normal.
When I run the command /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml normally in the shell it works fine and the metrics are sent to my other server.

/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 is the location of the bin for the scrutiny data collector.

Any idea what I am doing wrong or why it is not working?

Thanks in advance!

EDIT: extra information
 
Hello,

every minute would be */1 * * * * .
Give it a try.

Kind regards,


--
Guillaume Delanoy
Thanks for the reply.
AFAIK * * * * * is the same thing as */1 * * * *.

When I run systemctl status cron I can see that the command runs every minute, but nothing seems to happen. This is the output of systemctl status cron:
Code:
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; preset: enabled)
     Active: active (running) since Thu 2023-11-23 21:27:11 CET; 12h ago
       Docs: man:cron(8)
   Main PID: 26349 (cron)
      Tasks: 1 (limit: 38148)
     Memory: 564.0K
        CPU: 14.696s
     CGroup: /system.slice/cron.service
             └─26349 /usr/sbin/cron -f

Nov 24 10:21:01 platinum CRON[172012]: pam_unix(cron:session): session closed for user root
Nov 24 10:22:01 platinum CRON[172235]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Nov 24 10:22:01 platinum CRON[172236]: (root) CMD (/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny>
Nov 24 10:22:01 platinum CRON[172235]: pam_unix(cron:session): session closed for user root
Nov 24 10:23:01 platinum CRON[172436]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Nov 24 10:23:01 platinum CRON[172437]: (root) CMD (/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny>
Nov 24 10:23:01 platinum CRON[172436]: pam_unix(cron:session): session closed for user root
Nov 24 10:24:01 platinum CRON[172608]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Nov 24 10:24:01 platinum CRON[172610]: (root) CMD (/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny>
Nov 24 10:24:01 platinum CRON[172608]: pam_unix(cron:session): session closed for user root
 
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml
Change to:

Code:
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml >/tmp/error.log 2>&1

and inspect what's going wrong. Most of the time it's the restricted PATH environment in Cron and that it does not find programs.
 
Change to:

Code:
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml >/tmp/error.log 2>&1

and inspect what's going wrong. Most of the time it's the restricted PATH environment in Cron and that it does not find programs.
Oh that's a great suggestion. I did this and got this strange output.
Code:
root@platinum:~# cat /tmp/error.log
2023/11/24 12:42:01 No configuration file found at /opt/scrutiny/config/collector.yaml. Using Defaults.

 ___   ___  ____  __  __  ____  ____  _  _  _  _
/ __) / __)(  _ \(  )(  )(_  _)(_  _)( \( )( \/ )
\__ \( (__  )   / )(__)(   )(   _)(_  )  (  \  /
(___/ \___)(_)\_)(______) (__) (____)(_)\_) (__)
AnalogJ/scrutiny/metrics                        linux.amd64-0.7.2

2023/11/24 12:42:01 Loading configuration file: /opt/scrutiny/bin/collector.yaml
time="2023-11-24T12:42:01+01:00" level=info msg="Verifying required tools" type=metrics
2023/11/24 12:42:01 ERROR: DependencyMissingError: "smartctl binary is missing"
The two things I notice are as follows:
  1. It says that the config file is missing at path /opt/scrutiny/bin/collector.yaml, but it is there and works fine when I run /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml from the shell.
  2. It says that smartctl is missing, but it is most definetely installed. I get correct output when I run smartctl --version. Also again, the command runs perfectly fine when I run it from shell.
Do you know what might cause this? Thanks!
 
It says that the config file is missing at path /opt/scrutiny/bin/collector.yaml, but it is there and works fine when I run /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml from the shell.
Please post the output of

Code:
ls -l /opt/scrutiny/bin/collector.yaml

Do you know what might cause this? Thanks!
As already suspected, the PATH environment. Please check where the smartctl binary is installed.
 
Please post the output of

Code:
ls -l /opt/scrutiny/bin/collector.yaml


As already suspected, the PATH environment. Please check where the smartctl binary is installed.

The output of the ls command
Code:
root@platinum:~# ls -l /opt/scrutiny/bin/collector.yaml
-rw-r--r-- 1 root root 3544 Nov 23 19:07 /opt/scrutiny/bin/collector.yaml

This is where the smartctl bin is installed. Should be the normal place as this is a fairly new Proxmox install (a few days old) and I havent really messed with anything (I think).
Code:
root@platinum:~# which smartctl
/usr/sbin/smartctl

EDIT:
What do you mean by PATH environment? I don't really understand how that fits in the command working from shell and not from cron. I've never had this issue on RHEL based distro's (which I am more accustomed to).

Anyway, thank you so much for your help!
 
Last edited:
What do you mean by PATH environment? I don't really understand how that fits in the command working from shell and not from cron. I've never had this issue on RHEL based distro's (which I am more accustomed to).
If you run scripts in cron, a lot of environment variables is not set as they would be in an interactive shell.

The easiest way to debug this would be to create a shell script /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh with the following contents:

Code:
#!/bin/bash

id
echo $PATH

/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml

make it executable and just run it from cron:

Code:
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh >/tmp/error.log 2>&1

and start debugging.
 
If you run scripts in cron, a lot of environment variables is not set as they would be in an interactive shell.

The easiest way to debug this would be to create a shell script /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh with the following contents:

Code:
#!/bin/bash

id
echo $PATH

/opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64 run --config /opt/scrutiny/bin/collector.yaml

make it executable and just run it from cron:

Code:
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh >/tmp/error.log 2>&1

and start debugging.

Okay I think I understand. I have made the .sh file. Running just that file works just fine. Looks like the error message of the not found config file can be ignored. It uses the correct config variabeles I set when I run the command.

Code:
root@platinum:~# /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh
uid=0(root) gid=0(root) groups=0(root)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2023/11/24 13:16:29 No configuration file found at /opt/scrutiny/config/collector.yaml. Using Defaults.

 ___   ___  ____  __  __  ____  ____  _  _  _  _
/ __) / __)(  _ \(  )(  )(_  _)(_  _)( \( )( \/ )
\__ \( (__  )   / )(__)(   )(   _)(_  )  (  \  /
(___/ \___)(_)\_)(______) (__) (____)(_)\_) (__)
AnalogJ/scrutiny/metrics                        linux.amd64-0.7.2

2023/11/24 13:16:29 Loading configuration file: /opt/scrutiny/bin/collector.yaml
INFO[0000] Verifying required tools                      type=metrics
INFO[0000] Executing command: smartctl --scan --json     type=metrics
INFO[0000] Executing command: smartctl --info --json /dev/sda  type=metrics
INFO[0000] Generating WWN                                type=metrics
INFO[0000] Sending detected devices to API, for filtering & validation  type=metrics
INFO[0000] Collecting smartctl results for sda           type=metrics
INFO[0000] Executing command: smartctl --xall --json /dev/sda  type=metrics
INFO[0000] Publishing smartctl results for 0x5002538e90906fa8  type=metrics
INFO[0000] Main: Completed

Edited the crontab:
Code:
root@platinum:~# crontab -l
@reboot (sleep 60 && echo "powersave" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)

# Run scrutiny metrics collector and export to Sapphire
# Every minute
* * * * * /opt/scrutiny/bin/scrutiny-collector-metrics-linux-amd64.cron.sh >/tmp/error.log 2>&1

Now I get the same output in the error log.
Code:
root@platinum:~# cat /tmp/error.log
uid=0(root) gid=0(root) groups=0(root)
/usr/bin:/bin
2023/11/24 13:18:01 No configuration file found at /opt/scrutiny/config/collector.yaml. Using Defaults.

 ___   ___  ____  __  __  ____  ____  _  _  _  _
/ __) / __)(  _ \(  )(  )(_  _)(_  _)( \( )( \/ )
\__ \( (__  )   / )(__)(   )(   _)(_  )  (  \  /
(___/ \___)(_)\_)(______) (__) (____)(_)\_) (__)
AnalogJ/scrutiny/metrics                        linux.amd64-0.7.2

2023/11/24 13:18:01 Loading configuration file: /opt/scrutiny/bin/collector.yaml
time="2023-11-24T13:18:01+01:00" level=info msg="Verifying required tools" type=metrics
2023/11/24 13:18:01 ERROR: DependencyMissingError: "smartctl binary is missing"

Edit:
I notice a difference at the start of the outputs. When running from the shell it says:
Code:
uid=0(root) gid=0(root) groups=0(root)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
And when run from crontab it says:
Code:
uid=0(root) gid=0(root) groups=0(root)
/usr/bin:/bin
I assume that this is the path environment problem you mentioned? Any way around this?
 
Last edited:
I assume that this is the path environment problem you mentioned? Any way around this?
Yes, exactly. Just add the following to the cron-sh-file:

Code:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

before the command and you're good to go.


This is actually the same on RHEL:

Code:
root@ol8-oracle ~ > crontab -l
* * * * * /root/test-cron.sh >/tmp/error.log 2>&1


root@ol8-oracle ~ > cat test-cron.sh
#!/bin/bash

cat /etc/redhat-release
uname -a
echo $PATH


root@ol8-oracle ~ > cat /tmp/error.log
Red Hat Enterprise Linux release 8.8 (Ootpa)
Linux ol8-oracle 4.18.0-477.13.1.el8_8.x86_64 #1 SMP Tue May 30 16:09:32 PDT 2023 x86_64 x86_64 x86_64 GNU/Linux
/usr/bin:/bin
 
Yes, exactly. Just add the following to the cron-sh-file:

Code:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

before the command and you're good to go.


This is actually the same on RHEL:

Code:
root@ol8-oracle ~ > crontab -l
* * * * * /root/test-cron.sh >/tmp/error.log 2>&1


root@ol8-oracle ~ > cat test-cron.sh
#!/bin/bash

cat /etc/redhat-release
uname -a
echo $PATH


root@ol8-oracle ~ > cat /tmp/error.log
Red Hat Enterprise Linux release 8.8 (Ootpa)
Linux ol8-oracle 4.18.0-477.13.1.el8_8.x86_64 #1 SMP Tue May 30 16:09:32 PDT 2023 x86_64 x86_64 x86_64 GNU/Linux
/usr/bin:/bin
you are a saint, that fixed it. Thank you so much!

Interestingly I've never run into this issue before. Guess I must not have tried something like this before.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!