LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-11-2016, 04:02 PM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 14 & 15
Posts: 80

Rep: Reputation: Disabled
Script wont run from cron


I have a script that when executed checks to see if a certain drive is mounted, if it is not, the script then attempts to mount it. If I run the script from the terminal it works a treat, however I can't get it to work from Cron.

BTW, The script will prompt me for my password each time it's run.

The script is called auto_mount_nas.sh and lives in /home/tim/bin The line I use in cron is as follows:

Code:
*/5 * * * * sh /home/tim/bin/auto_mount_nas.sh
I thought it may not run from cron as it requires a password, therefore I put the same bit of code in the sudo cron (sudo crontab -e). Unfortunately it still does not work.

Please let me know what I am doing wrong.

Many thanks
Tim
 
Old 04-11-2016, 04:07 PM   #2
Emerson
LQ Guru
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~arch
Posts: 5,872

Rep: Reputation: Disabled
You need to include full path to sh, cron is not running in your user environment and cannot find commands without full path.
 
Old 04-11-2016, 04:21 PM   #3
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 14 & 15
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Emerson View Post
You need to include full path to sh, cron is not running in your user environment and cannot find commands without full path.
OK thanks. What would the full path look like? Would it be /root/home/tim/bin ?

Many thanks
Tim
 
Old 04-11-2016, 04:43 PM   #4
Emerson
LQ Guru
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~arch
Posts: 5,872

Rep: Reputation: Disabled
Code:
~ $ which sh
/bin/sh
 
Old 04-11-2016, 04:52 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,265

Rep: Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624
A cron job is not associated with a terminal window so you will never see the password prompt. You could use zenity to display a popup message to enter the password and run your script like:

*/5 * * * * env DISPLAY:=0.0 myscript.sh

However, does the share unmount for some unknown reason? A better solution might be to use autofs so that when you access the share mount point it will automatically mount if needed.
 
1 members found this post helpful.
Old 04-13-2016, 12:53 PM   #6
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 242
Blog Entries: 2

Rep: Reputation: 48
What Michael said: this does not seem the job for cron. autofs or udev-rules should suffice to mount it.
Why is it asking for a password though? You might be able to provide the password through the script, or run the relevant part with higher permissions.
 
Old 04-15-2016, 04:43 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Also, as hinted above, you'd also have to specify the full path to all the cmds inside the shell script (if you did it that way).
 
Old 04-15-2016, 06:31 AM   #8
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,796

Rep: Reputation: 619Reputation: 619Reputation: 619Reputation: 619Reputation: 619Reputation: 619
Why reinvent the horse?

Quote:
Originally Posted by chrism01 View Post
Also, as hinted above, you'd also have to specify the full path to all the cmds inside the shell script (if you did it that way).
Alternate: put a full path into the script.
Code:
export PATH=/bin:/usr/bin:/usr/local/bin:${HOME}/bin
as an example.

Still, even if you make this script work, something like autofs is the better answer.
 
  


Reply

Tags
bash, cron, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cron wont run a shellscript Osbian Linux - General 2 12-23-2013 03:19 AM
script run fine from ./ but when put in cron does not run j-me Linux - Server 6 06-16-2012 11:02 AM
rsyncbackup wont run over cron ulver Linux - Software 1 12-24-2009 11:42 AM
Cron wont run my shell script shelb Linux - Desktop 4 05-27-2007 10:55 PM
Cron restart script wont work! ThaHabbis Linux - General 3 03-16-2007 05:38 AM


All times are GMT -5. The time now is 07:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration