0

The following is my cronjob

* * * * * metrics.sh 5953c0685932c30ccbf7d916 5953c0e35932c30ccbf7d917

And here is my metrics.sh

d_t=$(df | grep "/$" | awk '{print $2;}')
d_u=$(df | grep "/$" | awk '{print $3;}')
m_t=$(free | grep Mem | awk '{print $2;}')
m_u=$(free | grep Mem | awk '{print $3;}')
curl -H "Content-Type: application/json" -X POST -d '{"d_t":'$d_t',"d_u":'$d_u',"m_t":'$m_t',"m_u":'$m_u'}' https://localhost:3000/server/metrics/$1/$2
  • Put the full path name in front of metrics.sh. Also you aren't specifying when cron should run the job. How often do you want it to run? – WinEunuuchs2Unix Jun 28 '17 at 17:09
  • I want the cron to run every minute hence I have put 5 stars and the path where metrics.sh resides is actually in a directory outside root. So basically cd /directory/metrics.sh – Hari Aakash Jun 28 '17 at 17:11
  • Don't put in the cd just the full path i.e.:/user/local/bin/metrics.sh. Let me know if that fixes it and I'll post a proper answer for others to see. – WinEunuuchs2Unix Jun 28 '17 at 17:14
3

metrics.sh must be executable from a CLI as a first step in troubleshooting your issue. Common failure causes is the script not being executable or a coding mistake in the script.

ls -l metrics.sh

Should have the x flag set for the correct owner, group or world.

./metrics.sh

Should run error free or will provide error messages that indicate coding or privilege problems.

Cron jobs run more reliably when using fully qualified directives and not assuming that all variables will be passed to cron. ex:

* * * * * $HOME/metrics.sh

or

* * * * * /home/user/scripts/metrics.sh

Your script is elegant and I see no obvious programming errors. However, note that bash scripts should have the following first line:

#!/bin/bash

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.