1

I set up a cronjob to write a webpage every 50 minutes, but when accessing them, the webpages were blank.

The cronjob: 50 * * * * mkIndex.sh > /home/user/img/index.html.

The mkIndex.sh is a script I wrote, in /home/$USER/bin, which is in the $PATH, running the script in the same way shown in crontab seems to work fine, it generates the webpage.

So why is my index.html file blank? Alternatively, can you see any flaw in my script?

#!/bin/bash
echo -e "<!DOCTYPE html>\n<head>\n"
echo -e "<style>\ndiv.gallery {\nmargin: 1px;\nborder: 1px solid #ccc;\nfloat: left;\n"
echo -e" width: 180px;\n}\n\ndiv.gallery:hover {\nborder: 1px solid #777;\n}\n\ndiv.gallery img {\n"
echo -e "width: 10%;\nheight: auto;\n}\n"
echo -e "</style>\n</head>\n"
echo -e "<body>\n"
 
cd /home/user/img
for i in `ls *.png`
do
echo -e "<div class="gallery">"
echo -e  "<a target="$i" href="$i">"
echo -e   " <img src="$i"  width="300" height="200">"
echo -e  "</a>"
echo -e "</div>"
done
 
echo -e "</body>\n</html>"

I'm not sure why my cronjob isn't generating the desired output.

João Paulo
  • 127
  • 7
j0h
  • 14,548
  • 28
  • 104
  • 178
  • 1
    Silly question, but is the cron job set up to run from `root` or from your account? – matigo Jan 04 '23 at 08:30
  • omg yeah. Can a non priviliged user run cronjobs? – j0h Jan 04 '23 at 08:35
  • so like this then? $crontab -e -u user 50 * * * * user mkIndex.sh > /home/user/img/index.html I'll see how that goes. – j0h Jan 04 '23 at 08:40
  • "*Alternatively, can you see any flaw in my shell script?*" ... Yes, :-) ... Parsing the output of `ls` with `for i in $(ls *.png)` ... please see https://askubuntu.com/a/1446578/968501 for more information ... Plus the back-ticks for command substitution are deprecated in favor of `$( ... )` – Raffa Jan 04 '23 at 08:44
  • For your original question as @matigo hinted above ... You want to always use the full absolute path to your `mkIndex.sh` script in the cron entry and probably even better provide the full path to the executing shell executable file like `/bin/bash /home/user/mkIndex.sh ...`. – Raffa Jan 04 '23 at 08:54
  • 2
    `/home/$USER/bin` may well be in your PATH in an *interactive* login, but almost certainly is not in cron's PATH. See [here](https://askubuntu.com/a/919512/178692) for example. – steeldriver Jan 04 '23 at 13:36
  • will running `# run-parts /etc/cron.hourly/` execute the task? or do I need to put a link in that folder? – j0h Jan 07 '23 at 01:40

1 Answers1

0

I added a link to the script in /bin where root can use it. That alone did not work, after editing ctrontab -e every way I could imagine, I deleted my entry there, and edited the file /etc/crontab to include a line for the script with root as the user.
50 * * * * root mkIndex.sh > /home/user/img/index.html

That worked.

j0h
  • 14,548
  • 28
  • 104
  • 178