0

New to Crontab use, I am trying to get a simple bash script to run.

add_temp:

#!/bin/bash

rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"

echo $rnd1
echo $rnd2

cd /var/www/html

sqlite3 test.db <<EOF

INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );

EOF

crontab -e:

SHELL:/bin/bash

* * * * * /var/www/html/add_temp

This doesn't seem to run at all. Works fine if run manually with /var/www/html/add_temp.

chivs890
  • 27
  • 4
  • 1
    try to do /usr/bin/echo or /bin/echo depending upon where is your echo is installed. and try if that works – Rishikesh Jha Aug 22 '18 at 12:31
  • Otherwise add &>> at the end and redirect it to file. and see if anything is breaking – Rishikesh Jha Aug 22 '18 at 12:33
  • 2
    normally, after each run, cron will send you an email with information if something went wrong. Use the command `mail` to have a look at these. They might contain information about what is wrong. – kvantour Aug 22 '18 at 12:35
  • @chivs890, could you please try to add `SHELL=/bin/bash` in crontab and then try once? Let me know how it goes then? – RavinderSingh13 Aug 22 '18 at 12:35
  • @RavinderSingh13 The writing a .txt file seems to be working now! However, when trying to run my shell script with * * * * * /var/www/html/add_temp nothing happens. Script runs fine when run manually though :/ – chivs890 Aug 22 '18 at 12:56
  • @chivs890, could you please do let me know if you need to run script every min or on a specific time only? – RavinderSingh13 Aug 22 '18 at 13:13
  • @RavinderSingh13 I want it to run every minute, and currently have SHELL=/bin/bash and * * * * * /var/www/html/add_temp written in crontab -e. – chivs890 Aug 22 '18 at 13:16
  • @chivs890, please DO NOT post code in comments, comments are NOT meant for it, post them in your question itself. – RavinderSingh13 Aug 22 '18 at 13:19
  • @RavinderSingh13 My apologies, have posted it in the OP question. – chivs890 Aug 22 '18 at 13:21
  • @RavinderSingh13 It appears the issue was the folder it was in. Placing the add_temp script into /tmp/ folder allows it to run. Is there a way I can allow it to run from /var/www/html/ ? – chivs890 Aug 22 '18 at 13:27
  • @chivs890, please check the permissions on the file by doing `ls -ld file_name` and see if execute permissions are there or not? – RavinderSingh13 Aug 22 '18 at 13:30
  • For security purposes, it is quite reasonable to configure a system so that the cron daemon does not even have read access to /var/www. Why don't you put your script somewhere more reasonable? (eg, /usr/bin) – William Pursell Aug 22 '18 at 13:33

1 Answers1

0

My guess is that the sqlite3 is not found when the cron daemon run the script. You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:

type sqlite3

The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script. The variable that is usually differently set is PATH.

The first step to troubleshoot this situation is to compare the environments and debug the script.

I suggest to insert these lines below, after the very first line of the script

env
set -x

Run the script manually and redirect output, from a terminal:

/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1

Change the crontab line for:

* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1

Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.

When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.

Jay jargot
  • 2,745
  • 1
  • 11
  • 14