19

How to run a cron with bash script here.What i did as follows and here with errors.I want to know how to do that in ubuntu.I was struck with it now

bash.sh file

#!/bin/bash
cd /var/www/Controller
/usr/bin/php post.php

In crontab -e

* * * * * /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1

But now i getting following error

/bin/sh: 1: /home/samitha/bash.sh: Permission denied

How will i fix it ? what i did wrong ?

underscore
  • 6,495
  • 6
  • 39
  • 78

6 Answers6

22

you can try the following solution as well:

chmod +x post.php
chmod +x bash.sh
echo "* * * * * /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1" >> cronjob

chmod +x cronjob

/etc/init.d/crond start  #redhat based servers like centos
/etc/init.d/cron  start  #debian based servers like ubuntu

crontab cronjob
MLSC
  • 5,872
  • 8
  • 55
  • 89
3

The problem could be that your user does not have the rights to execute the file.

First you set the execution flag for your script

chmod +x /home/samitha/bash.sh

Then you should check the permissions for the php file with

ls -lah /var/www/Controller

If neither your usergroup nor your username shows up, you have to run the script with superuser rights or change its permissions.

First way would be put your entry in

sudo crontab -e

or the second one would be (which I would not recommend, as everyone would be allowed to execute the script by calling your site)

 chmod a+x /var/www/Controller/post.php
Heiko Becker
  • 556
  • 3
  • 16
2

The user executing that cron (the one who executes cron -e) doesn't have proper rights for executing that script. That is: either the script lacks the execution flag, or it's not possible to reach it because some of its ancestor directories lack the execution flag.

1

TL;DR: Insert "bash" before the script in crontab and in any scripts called by the script.

I have a fix for this. None of the previous answers worked for me. I have two Asus laptops running Kubuntu (updated to kernel v5.8) with virtually identical configurations. I don't know why one has the problem and the other doesn't. However, after 2 days of experimentation I found a fix. Hopefully, someone more knowledgeable than I can find the cause.

Cron is using sh instead of bash. I tried adding SHELL=/bin/bash and defining PATH above the commands in crontab and it had no effect. All of my scripts have the #!/bin/bash shebang at the beginning, also with no effect. My scripts' (and their directories') permissions are 777. The scripts don't run for cron or users, no matter what combination of user:group I tried on the files. Using full pathnames is cron and inside the scripts had no effect that was different from using environment variables.

My fix was to insert "bash" before the script filename in crontab. E.g.:

00 01 * * * bash $BASH_SCRIPTS/backup_os.sh

(Yes, cron has no problem with using my environment variables defined in /etc/environment.) Also, in cron, when a script runs another script, the second script will get "permission denied" unless that script is modified to have "bash" before the 2nd script's filename, or use "source" if that'll work with your script.

TaoDoggy
  • 11
  • 1
1

I had exactly the same error as the OP, but on RHEL. Of course it was not a cron issue, but permissions. I should have checked this by trying to run the script manually, but I did not, because it worked well on other servers and file permissions were OK. Eventually, after a couple of hours scratching my head, I figured that the cause was that on this particular server the FS with home directory was mounted with noexec option (use for example mount | grep home to check mount options for /home). Another detail to keep in mind.

EDIT: I read some more and it turns out that using noexec on the FS with home directories is now advisable for "security" reasons (although the point of doing this is debatable). I also realized that probably yendao42 had the same problem, because indeed adding bash in front of bash scripts is a very simple workaround for this exact issue.

Wojtek K.
  • 11
  • 3
0
  • File must be executable (@see chmod)
  • All parent directories must have the execution flag (@see chmod)
  • If crontab is running by different user i.e. not owner, maybe this user don't have rights to execute. (@see chown)