Crontab executes shell script: Mount error(13): Permission denied - linux

I have got a RasPi and I actually try to execute a shellscript to automount a folder at every Reboot.
Script Command is:
sudo mount -t cifs 'folderpath' 'pointtomount' -o username=xxx,password=xxx,sec=ntlm
It works perfect if I use it manually but via cronjob it responses "Mount Error(13): Permission denied" and the mount can't be executed.
Means cronjob executes the file at least.
My idea was to mount it manually and check if automount is disabled in /etc/fstab or /etc/mtab. As it's just a folder I only found it in mtab.
I can't write in it but nowhere's "noauto" in the options so probabbly everything is correct.
Not certain if it has sth. in common with crontab execute rights but ls -lha /usr/bin/crontab output is -rwxr-sr-x 1.
If anyone of you got any clues how to solve this problem, i'd appreciate help.
Thanks
EDIT1:
Okay after hours and hours it seems to be working in /home/pi/.config/lxsession/LXDE-pi/autostart.sh (type "sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart.sh"). In that file I wrote "#/home/pi/scripttoexecute.sh". In my executescript I wrote "sudo mount -t cifs 'foldertomount' 'directorypath' -o credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777,sec=ntlm". Obviously to use the smbcredentials file, write "sudo nano /root/.smbcredentials" and in there "username=xxx" and next line "password=xxx" and optional domain.
Thanks to all and I hope that this might save someone elses time.
Not sure if it has sth in common with apt-get upgrade and apt-get update before.

Couple things here, first off every user can have their own crontab. For example:
crontab -e # Edit crontab of current user
crontab -u root -e # Edit crontab of root user (might need sudo for this)
crontab -u www-data -e # Edit crontab of www-data user
Another thing is that if you don't use crontab -e to edit the file, and actually edit the /etc/crontab file directly (do something like vim /etc/crontab), you can actually specify the user you'd like to run the cron as:
* * * * * root mount -t cifs /path/to/folder /point/to/mount -o username=xxx,password=xxx,sec=ntlm

To run via root's crontab at reboot, type:
sudo crontab -e
And add this line:
#reboot mount -t cifs 'folderpath' 'pointtomount' -o username=xxx,password=xxx,sec=ntlm
But really, shouldn't you be adding your auto-mounts to /etc/fstab?

Related

cron: Run .sh as root

To make things clear first: I'm using a virtual machine which is offline - security is therefor no showstopper for my need.
I have a simple bash script which performs which uses wmctrl to gracefully closes a process. KILL is not an option:
* * * * * sudo wmctrl -xc notepadqq-bin.Notepadqq
* * * * * /bin/echo "works" >> /home/usr/cron.log
I tried sudo crontab -e as well as crontab -e, the cron.log is written every minute, however the wmctrl command is not working.
I even tried to echo "password" | sudo -S rm somefile but this doesnt work for me either.
TIA!
First I'm going to make some assumptions (please correct me if these are wrong and I'll try to adapt the answer for you)
Assumptions:
you want to use cron and not any old job manager (I think this is safe from your question)
you are already familiar with cron and would like the cronjobs to actually run as often as possible (this seems odd to me but may not be central to the question at hand) ( Edit:
I also agree with that other guy's recommendation to look at https://stackoverflow.com/tags/cron/info as it provides more in depth discussion on the use of cron )
you want to use sudo for root privileges and not have the cron daemon run as root (which is normal on most systems)
you are not satisfied with chown 0:0 $(which wmctrl) the effect of chmod 4755 $(which wmctrl) or more restrictive chmod 4755 $(which wmctrl)
the results of a cron tab line "* * * * * sudo wmctrl -d >> /home/usr/cron.log" shows this "notepadqq-bin.Notepadqq" is still active from cron's perspective (IF THIS IS NOT THE CASE check the environment variables for wmctrl are preserved from cron't perspective. and let me know this assumption was wrong)
your comment "even tried to echo "password" | sudo -S rm somefile but this doesn't work for me either." implies you have a password for using sudo
You mean your comment "security is therefor no showstopper for my need"
Ensure Sudoers allows the script call
Given those assumptions, I'd recommend first looking into how sudo is setup:
Try this:
(DO NOT EDIT THIS FILE WITHOUT THE visudo command you can corrupt your system)
export EDITOR=vi ; sudo visudo -f /etc/sudoers
you will want a line similar to this:
%sudo ALL=(ALL) NOPASSWD: ALL
OR
cron ALL=(ALL) NOPASSWD: ALL
given that you can be sure the issue is not a password prompt issue with sudo and that the command (infact ALL commands run via sudo) are run as root by default (when not using -u to set to another user)
Hope this helps

Linux script is unable to delete files via crontab, but it works manually

I have a simple script file to copy all files to a remote server and then delete them all. I could run this script by "user" manually, when i add into crontab (user), the first part, scp, works fine, but the rm part is always with failure.
i wonder what i am missing or set up incorrectly, could somebody help me out with this ?
thanks in advance
/home/user/bin/test.sh
#!/bin/bash
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio&&sudo rm -f /var/spool/asterisk/monitor/*
access permission of /var/spool/asterisk/monitor
drwxr-xr-x. 1 root root 532 Sep 06 11:14 monitor
crontab - user]
* */1 * * * bash /home/user/bin/test.sh
try this, it will work if sudo does not require password ( and it is possible ) )
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio && ssh test#xx.xx.xx.xx "sudo rm -f /var/spool/asterisk/monitor/*"
Make sure requiretty is off in /etc/sudoers. It is normally on by default on Red Hat.

Incron job is not being executed

I am using incron to monitor one of my file in /var/www/html directory.
output of incrontab -l
/var/www/html/test IN_ACCESS /home/intel/test.sh
This job is supposed to create a file in home directory, But when this job got executed (I opened the web address in browser), no file is created, following line is whon shown in /var/log/cron file
Jan 20 10:27:57 localhost incrond[26442]: (root) CMD (/home/intel/test.sh)
This clearly shows that event had occurred.
P.S: If I just run a /home/intel/test.sh in CLI its works fine and creates test file, following is my test.sh file.
#!/bin/bash
touch fm00
Mostly this problem occurs due to script file permission and ownership of script files. The same problem was faced by me. I found that my scrip owner was not a super user e.g. root.
So, you have to set the permission and ownership of your scrip as super user. Find below.
First of all edit your crontab as super user.(in RHEL like below)
[abc#host] crontab -e
and save crontab :wq!
Now set permission for script
[abc#host] chmod +x script.sh
[abc#host] chown root:root script.sh
Now restart your crontab.(in RHEL like below)
[abc#host] /etc/init.d/crond restart

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.

How to give permission for the cron job file?

I have set the cron tab for my site. But I have got message in my mailing id like this "Permission denied" for the script. Can anyone help me telling what may be the problem.
Thanks......
You get this error while setting the crontab? or from a script running from the cron?
If while setting the crontab, try this:
You type: crontab -e
You get: -bash: /usr/bin/crontab: Permission denied
Problem: Your user is not in the cron group.
Solution: As root, edit the /etc/group file, find the cron group and add
your user to that line (the usernames are comma-separated).
Then re-login as your user.
Verify: Run command "groups". You should see "cron" in there.
(from http://www.parseerror.com/argh/crontab-e-Permission-denied.txt)
friends if any one wants schedule crons from other user
just do this
root user:-
ls /usr/bin/crontab
chmod 4755 /usr/bin/crontab
echo PATH
vi /etc/crontab
SHELL=/bin/bash
PATH=/usr/java/jdk1.5.0_22/bin:/root/bin:/usr/java/jdk1.5.0_22/bin:/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin
MAILTO=root
HOME=/
cd /etc/cron.d
create cron.allow file if not exist
vi cron.allow
root
other username
save and exist
su – username
/usr/bin/crontab -e
schedule here ……………….
I ran into this issue today and was baffled until I realized that the denied commands were SSH commands. I had forgotten that I was connecting with an SSH key that required a passphrase, so the real issue had nothing to do with cron in my case.
My solution was to create an additional key for this script with no passphrase (using ssh-keygen), install it on the remote server, and specify it in the script with the -i flag to the ssh commands.
ssh -i /path/to/id_rsa.no-passphrase user#remote command-to-run

Resources