3

I have this cronjob to run every minute

*/1 * * * * root sh /test.sh

My /test.sh logs the result of "top" and "free" command. It works fine when I run it manually on terminal with "sh /teste.sh" and saves the output to a nice file, however when the cron job runs it only saves the result of the command "free" below. Check this please:

printf "\n" >> "log_lojar_top_free.txt"
printf %s "$(date)" >> "log_lojar_top_free.txt"
printf '\t' >> "log_lojar_top_free.txt"
top -b -n 3 -d 1 | grep "Cpu" |  tail -n 1 | awk '/^%Cpu\(s\)/ {printf $2}' >> "log_lojar_top_free.txt"
printf '\t' >> "log_lojar_top_free.txt"
free | awk '/^Mem:/ {printf $7}' >> "log_lojar_top_free.txt"

What is the error that is causing that only the last line (free) to have it's output loged?

  • if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash. – cas Oct 31 '15 at 0:51
  • you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1. – gogoud Oct 31 '15 at 8:13
  • A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root. – roaima Nov 3 '15 at 22:51
6

As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.

Maybe this is the key to your problem.

EDIT: I've got it working with this crontab entry, done as root with crontab -e:

*/1 * * * * /bin/bash -c "/test.sh"
  • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash. – roaima Nov 3 '15 at 23:22
0

This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.

System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):

* * * * * root /root/test.sh

As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):

#!/bin/bash
top -b -n 3 -d 1 |
    awk -v date="$(date)" '
        /^%Cpu/ {cpu=$2}
        /cached Mem/ {cached=$9}
        END {printf "\n%s\t%s\t%s", date, cpu, cached}
    ' >> /root/log_lojar_top_free.txt

Make the script executable:

chmod +x /root/test.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.