1

I want to get the CPU percentage measured every minute and written to a file. The command is as follow: Code: Select all

* * * * * cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS='' '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}' > /log/mainboard_cpu.log

If I run the code manually, I get the CPU written to the file but the cronjob doesn't do anything. Is there another way to execute the command and write it to a file? Thank you.

I am running the code on the Raspberry Pi.

SharpC
  • 6,974
  • 4
  • 45
  • 40
BLB
  • 23
  • 1
  • 6

1 Answers1

1

Crontab does not have PATH well defined, so you may need to use full path for command, like /usr/bin/cat (use which command to identify path).

Starfight
  • 176
  • 2
  • 9
  • Thank you, i used the which command to figure out the path and it gave me /bin/cat. I updated the crontab but it still doesn't work – BLB Apr 25 '19 at 13:12
  • Have you any log in `/var/log/syslog` ? – Starfight Apr 25 '19 at 13:15
  • Apr 25 15:15:01 TRM-RF01-134138 CRON[2469]: (root) CMD (/bin/cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | /usr/bin/awk -v RS='' '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}' > /log/mainboard_cpu.log) Apr 25 15:15:01 TRM-RF01-134138 CRON[2455]: (CRON) info (No MTA installed, discarding output) ps: i also updated awk with /usr/bin/awk but still nothing – BLB Apr 25 '19 at 13:15
  • personally I would have put the complete path to all commands (including grep and sleep), or define PATH at start. – Starfight Apr 25 '19 at 13:19
  • Could you help me with that? I am willing to do that but i have no idea how to update this command (this is from internet but does exactly what i need) – BLB Apr 25 '19 at 13:22
  • This link can help you to define PATH for all crontab : https://stackoverflow.com/a/2409369/3489104 If you want only for this command, simply add `PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;` before your command line. – Starfight Apr 25 '19 at 13:26
  • Thank you for the help but it doesn't work yet. I added your line or the lines from the example and tried different combinaties wit cat, /bin/cat etc. but it doesn't create the file – BLB Apr 25 '19 at 13:40
  • Maybe it's not the only problem, your log in syslog indicate you do not have a mail command to transfert explicite logs. You can install mailx for that. – Starfight Apr 25 '19 at 13:51
  • Now i did, but which email is used for the crontabs, the one i setup as root email? This is the syslog output: Apr 25 15:59:03 TRM-RF01-134138 cron[305]: sendmail: 550 5.7.1 Command rejected Apr 25 15:59:03 TRM-RF01-134138 sSMTP[10595]: 550 5.7.1 Command rejected Apr 25 15:59:03 TRM-RF01-134138 CRON[10560]: (root) MAIL (mailed 19 bytes of output but got status 0x0001 from MTA#012) – BLB Apr 25 '19 at 14:01
  • mailx work with sendmail, you can configure MAILTO var in crontab, but mail can also be logged on system. May you have also more logs in `/var/cron/log` – Starfight Apr 26 '19 at 08:12
  • Thanks but /var/cron/log is empty. i have setup the mail function but it doesn't send any mail. Is there another way to get this cronjob working? – BLB Apr 26 '19 at 10:10
  • You need log information to go further, may you have pending mail `sudo mailq` ? – Starfight Apr 26 '19 at 13:03