Browse Category

Bash

Bash Script Not Writing to File

I was making a bash script, cron_its_servicenow.sh, that uses cURL and regex to grab 6 chart images (that are encoded in base64) from 6 ServiceNow published reports, and then writing them to a file, page_its.php. Here’s the code:

#!/bin/bash

file=/var/www/dashboard/it/page_its.php

urls=(
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*1*"
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*2*"
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*3*"
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*4*"
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*5*"
	"https://*.service-now.com/sys_report_display.do?sysparm_report_id=*6*"
)

for (( i = 0 ; i < ${#urls[@]} ; i++ )) do
	result[$i]=`curl -s ${urls[$i]} | sed -rn 's/.*<img usemap="#" class="chart" src="(.+)"><\/img>.*/\1/p'`
done

echo '<div class="row shadow">' > $file
echo '<h3>IT Services</h3>' >> $file
echo '<div class="col-md-4"><img src="'${result[0]}'" class="img-responsive" /></div>' >> $file
echo '<div class="col-md-4"><img src="'${result[1]}'" class="img-responsive" /></div>' >> $file
echo '<div class="col-md-4"><img src="'${result[2]}'" class="img-responsive" /></div>' >> $file
echo '</div>' >> $file
echo '<div class="row shadow">' >> $file
echo '<div class="col-md-4"><img src="'${result[3]}'" class="img-responsive" /></div>' >> $file
echo '<div class="col-md-4"><img src="'${result[4]}'" class="img-responsive" /></div>' >> $file
echo '<div class="col-md-4"><img src="'${result[5]}'" class="img-responsive" /></div>' >> $file
echo '</div>' >> $file

It was working fine when I was running it in SSH like this:

root@server ~# /var/www/dashboard/it/cron_its_servicenow.sh

No errors, and the page_its.php file was getting the data written to it.

But then I logged into the web control panel and set up the Scheduled Cron Job, all these errors popped up when I ran the cron job:

Execute Cron Job
Output from command /var/www/dashboard/it/cron_its_servicenow.sh ..

/var/www/dashboard/it/cron_its_servicenow.sh: line 18: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 19: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 20: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 21: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 22: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 23: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 24: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 25: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 26: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 27: /var/www/dashboard/it/page_its.php: Permission denied
/var/www/dashboard/it/cron_its_servicenow.sh: line 28: /var/www/dashboard/it/page_its.php: Permission denied

What??

I couldn’t find out what was wrong. I was trying things like changing echo to cat, adding tee, using sudo, etc. It turns out that it was such a simple mistake… I forgot to set the file permissions of page_its.php to be writable.

Solution: I did chmod 666 page_its.php, and all is working well now.

Execute Cron Job
Output from command /var/www/dashboard/it/cron_its_servicenow.sh ..

No output generated

Excellent!