Sunday, November 30, 2014

linux - Crontab running before nfs mounted



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.


Answer



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

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...