Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Running Cronjob @reboot returns that file on nfs share does not exist.

Example

@reboot python /abs/path/to/script.py

mail from crontab on startup reads "more or less"

/usr/bin/python can't open file "/abs/path/to/script.py": [Error No. 2] No such file or folder.

Script can be run from the command line with no trouble..
Theory is that the cronjob is running before mount has been run.
The questions.

  1. Is this theory correct?
  2. Is there a way to force the job to wail until the drive has been mounted? .... Other than just putting in a sleep 60 into the command. ;) I tried that already, but it's hit and miss and I need the script to run 100% of the time quickly.
share|improve this question
2  
What distribution are you using? –  SvW Oct 8 '12 at 17:08
add comment

4 Answers

Here are some ideas for you:

  1. check if NFS is mounted. If not, mount it, then run your script:

    [ ! -f /abs/path/to/script.py ] && mount -t nfs device dir && python /abs/path/to/script.py

  2. run your script in the start() function of the NFS init script:

    echo -n $"Starting NFS mountd: "
    daemon rpc.mountd $RPCMOUNTDOPTS
    RETVAL=$?
    echo
    [ $RETVAL -ne 0 ] && exit $RETVAL || python /abs/path/to/script.py
    
share|improve this answer
add comment

To be absolutely sure that the python script is able to run. You would need to wrap it in a script stored on local storage to verify that the mount point has come up.

Something like (warning pseudo code):

while (!ScriptExists && ErrorCount < 10)
do
    mount /my/mount/point
    sleep 10
    ErrorCount++
done
share|improve this answer
add comment

You can use the mountpoint command to ensure the mount has taken place before you execute your command e.g. (assuming /abs is the mount point)

#!/bin/bash
while true
do
    if mountpoint -q /abs
        then
            /usr/bin/python /abs/path/to/script.py
            break
        fi
    sleep 10
done
share|improve this answer
2  
+1 I've never heard of mountpoint - this is quite useful! –  SvW Oct 8 '12 at 17:29
    
@SvenW Same here. I'm now updating a handful of scripts with this. –  Aaron Copley Oct 8 '12 at 18:00
add comment

I'm not sure what the problem is since cron starts after networking. (At least in Red Hat and derivatives.) Are your mounts in /etc/fstab with _netdev option or else where?

The standard way to execute something on start up is to include it in /etc/rc.local. This will be ran after the network is initialized and all other services are started. (Including mounting of remote file systems.)

(Or is there a reason to only execute the script after a literal reboot?)

share|improve this answer
    
Yes the share is mounted in fstab. I was running in ubuntu, which means that the networking may not have been running. etc/rc.local does seem to be the best solution, but if @restart exists in crontab, the assumption is that it should work. –  Rusty Weber Apr 18 '13 at 6:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.