B
B
Bega2021-10-19 20:04:12
linux
Bega, 2021-10-19 20:04:12

The command in the bash script does not work, where is the error?

One of the commands in the script does not run when setting a task in cron

00 09 * * * /mnt/pve/backups/sensors.sh >> /root/myscript.log 2>&1


sensors work, hddtemp - no

13-Oct-2021	09:00

coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +29.0В°C  (high = +84.0В°C, crit = +100.0В°C)
Core 0:        +26.0В°C  (high = +84.0В°C, crit = +100.0В°C)
Core 1:        +26.0В°C  (high = +84.0В°C, crit = +100.0В°C)
Core 2:        +23.0В°C  (high = +84.0В°C, crit = +100.0В°C)
Core 3:        +23.0В°C  (high = +84.0В°C, crit = +100.0В°C)

acpitz-acpi-0
Adapter: ACPI interface
temp1:        +27.8В°C  (crit = +119.0В°C)
temp2:        +29.8В°C  (crit = +119.0В°C)


Error in root/myscript.log
/mnt/pve/backups/sensors.sh: line 9: hddtemp: command not found


The script itself
#!/bin/bash

date=`date +%d-%b-%Y%t%H:%M`

echo -e "\n$date\n" >> sensors.txt

sensors >> sensors.txt

hddtemp /dev/sda >> sensors.txt
hddtemp /dev/sdb >> sensors.txt
hddtemp /dev/sdc >> sensors.txt


PATH output
spoiler
[email protected]:~# type sensors
sensors is /usr/bin/sensors
[email protected]:~# type hddtemp
hddtemp is hashed (/usr/sbin/hddtemp)
[email protected]:~# $PATH
-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
No such file or directory

At manual start everything works. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Satisfied IT, 2021-10-19
@Bega

hddtemp /dev/sda >> sensors.txt
hddtemp /dev/sdb >> sensors.txt
hddtemp /dev/sdc >> sensors.txt

Use full paths to the hddtemp file.

S
Saboteur, 2021-10-19
@saboteur_kiev

You need to look at PATH not from your session, but directly from cron.
When a user logs in interactively, his personal profiles are executed - .profile, .bashrc, .login, etc.
Cron just runs with the rights of the specified user, without reading his profiles.
Options for how to fix this:
1. Use full paths to files in scripts
2. Set PATH directly in your script
3. Set PATH in the crontab itself at the beginning, for example

PATH=$PATH:/home/user/mypersonalbin/
00 09 * * * /mnt/pve/backups/sensors.sh >> /root/myscript.log 2>&1

crontab format allows you to set variables that will work for all scripts run from your crontab.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question