Search code examples
linuxshellcronmountpermission-denied

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


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.


Solution

  • 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