Permission denied with bash.sh to run cron

前端 未结 5 483
忘掉有多难
忘掉有多难 2020-11-29 12: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

相关标签:
5条回答
  • 2020-11-29 12:45

    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.

    0 讨论(0)
  • 2020-11-29 12:46

    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
    
    0 讨论(0)
  • 2020-11-29 12:46

    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
    
    0 讨论(0)
  • 2020-11-29 12:53
    • 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)
    0 讨论(0)
  • 2020-11-29 12:54

    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.

    0 讨论(0)
提交回复
热议问题