crontab permission denied error [duplicate] - linux

This question already has answers here:
Permission denied with bash.sh to run cron
(6 answers)
Closed 2 years ago.
I put a file.sh for crontab to execute and redirecting the output into an email to my mailox. The script works when I run it manually. Unfortunately, when crontab does the job, it has the following error (from the output sent to me):
/bin/sh: /home/zenoss/zen-remote-bkup.sh: Permission denied
My crontab setup is the following:
30 11 * * * /home/zenoss/zen-remote-bkup.sh 2>&1 | mail -s "Zenoss backup replication" email#abc.com
And these are the permission on the file.sh I need to execute:
-rw-rw-r-- 1 zenoss zenoss 1433 Nov 5 10:32 zen-remote-bkup.sh
[zenoss#server1 ~]$
Does anyone know which permission I am missing? Thank you.

You have to put the eXecutable bit on the script to do the job:
chmod +x /home/zenoss/zen-remote-bkup.sh

Related

Bash scripts only run when using bash command

Today, my working bash scripts stopped working on my Debian 11 server.
$ ./CCrec.sh
-bash: ./CCrec.sh: Permission denied
Fails even if using sudo.
Yes, the permissions are set correctly. (They've been working for years.)
$ ls -l CCrec.sh
-rwxr--r-- 1 user1 user1 858 Jan 23 20:30 CCrec.sh
Another clue: autotab it doesn't recognize the script is present with beginning with "./". This makes me think the's a change in my bashrc, but I'm not noticing a change.
script will run when specifying bash:
$ bash CCrec.sh
This is working
Any solutions?

bash: ./mybashfile.sh: Permission denied [duplicate]

This question already has answers here:
-bash: ./manage.py: Permission denied
(4 answers)
Closed 7 years ago.
I created a bash file mybashfile.sh containing below commands, but running it in the terminal giving me a Permission Denied Error I tried running the file from su but same Permission Denied Error for su.
I am running the command like this -
$: ./mybashfile.sh
It is in a directory containing mybashfile.sh
My mybashfile.sh file -
#!/usr/bin/env bash
redis-server --save "" &
sleep 1
redis-cli flushall
cd ~/Documents/class-prj/class-prj
npm run app.js
Let me know what I am doing wrong here.
Set the mode to executable:
chmod +x mybashfile.sh

tar archiving via cron does not work

I am trying to archive my localhost's root folder with tar and want to automate it's execution on a daily basis with crontab. For this purpose, I created a 'backupfolder' in my personal folder. I am running on Ubuntu 12.04.
The execution of tar in the command line works fine without problems:
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
However, when I schedule the command for a daily backup (let's say at 17.00) in sudo crontab -e, it is not executing, i.e. the backup does not update using the following command:
0 17 * * * sudo tar -cpzf backupfolder/localhost.tar.gz /var/www
I already tried the full path home/user/backupfolder/localhost.tar.gz without success.
var/log/syslog gives me the following output for the scheduled execution:
Feb 2 17:00:01 DESKTOP-PC CRON[12052]: (root) CMD (sudo tar -cpzfbackupfolder/localhost.tar.gz /var/www)
Feb 2 17:00:01 DESKTOP-PC CRON[12051]: (CRON) info (No MTA installed, discarding output)
/etc/crontab specifies the following path:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I assume that crontab is not executing as this is a sudo command.
Is there a way how I can get this running? What is the recommended, safe way if I don't want to hardcode my root password?
Well, the command that works for you is
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
Which means, you have to run the command with sudo access, and it will not work from within your crontab.
I would suggest adding the cron job to the root user's crontab.
Basically, do
sudo crontab -e
And add an entry there
0 17 * * * cd /home/user/backupfolder && tar -cpzf localhost.tar.gz /var/www
If that doesn't work, add the full path of tar (like /bin/tar).
Also, while debugging, set the cronjob to run every minute (* * * * *)
Basically the problem is the sudo command so we will allow sudo to run tar for the "user" without prompting for the password.
Add the following line in /etc/sudoers file.
user ALL=(ALL) NOPASSWD:/bin/tar
where user is the user installing the crontab.
I suspect a PATH problem, try to set some variables at the top of sudo crontab -e :
MAILTO=your_email#domain.tld # to get the output if there's errors
PATH=/usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/sbin
You can write your command in a script like run.sh
#/bin/sh -l
tar -cvpzf backupfolder/localhost.tar.gz /var/www
then use the crontab to run the script.
IMPORTANT NOTE: the script's first line has the "-l" option.
Try it.

Why does these sudo commands fail? [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 9 years ago.
I would like to add a crontab entry from a script as a normal user, so I use sudo to get root permissions, but fails no matter what I try.
$ sudo { crontab -u root -l; echo ' 15 9 * * * root /opt/script.sh'; } | crontab -u root
bash: syntax error near unexpected token `}'
$ sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab
bash: /etc/crontab: Permission denied
$ sudo echo ok
ok
Because you are running
sudo echo .......
as "su" then writing the result to /etc/crontab with:
>> /etc/crontab
so in the moment you are writing to /etc/crontab you're not "su" anymore
In sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab, sudo echo ' 15 9 * * * root /opt/script.sh' is ran first then the shell takes the output of the sudo command and appends it to /etc/crontab. Since the shell is started as a normal user and so doesn't have root privileges, the shell can't write to /etc/crontab, which only root can modify. To solve the problem one starts a subshell as root, which allows it to append to /etc/crontab. Fortunately, this has already been implemented as su -c, however since the system uses sudo, sudo has to be prepended. The fixed command is sudo sh -c "echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab"

Unable to edit file through sudo permission

I am trying to setup sudo permission for an account to have full access to all files under certain directory.
I have made the following entry in sudoers file
itg ALL= NOPASSWD: /apps/pgm/admin/
The file which I trying to modify and execute under /apps/pgm/admin/ path has the following permssion.
-rwxr--r-- 1 root root 6034 Oct 2 02:00 ptTomcat.ksh
Though I am able to execute the above script through sudo, when I try to edit this file through sudo I am getting the following error
Sorry, user itg is not allowed to execute '/bin/vi /apps/pgm/admin/ptTomcat.ksh' as root
I am running Linux
Any thoughts?
Your user cannot execute '/bin/vi' as root as you haven't added that command to /etc/sudoers for that particular user.
Try something similar to:
itg ALL= NOPASSWD: /bin/vi /apps/pgm/admin

Resources