LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-29-2015, 02:24 AM   #1
browny redandy
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Rep: Reputation: Disabled
Python Script Runs Perfectly on Bash but Fails at Cronjob


Hi,

I have been surfing for almost two days to look for related problem and solutions. But to no avail, I have not found any suitable ones. Any help is greatly appreciated!!

I am using Raspian on Rpi. I am running a python script pi_surveillance.py that requires an activation of the virtual environment cv before the script can work. I am implementing it on cronjob as I wanted it to run at every reboot.

The virtual environment is created for pi user. Hence, what I did was to create a launcher.sh script to activate the environment and run the python script.

Code:
#!/bin/bash
# launcher.sh
# activate the cv environment ,then execute
# python script


source ~/.profile 
workon cv

python  /home/pi/pi_surveillance.py --conf conf.json
For cronjob, what I did was,

Code:
sudo crontab -e
At the last line of the crontab, I added a line:

Code:
@reboot sudo -u pi /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
But, it did not work out and reflected this on my cron log.

Code:
Traceback (most recent call last):
  File "/home/pi/pi_surveillance.py", line 28, in <module>
    conf = json.load(open(args["conf"]))
IOError: [Errno 13] Permission denied: 'conf.json'
I am really desperate for a solution or a direction to where I should work towards!!

Last edited by browny redandy; 07-29-2015 at 02:25 AM.
 
Old 07-29-2015, 03:49 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Use a full path to the conf file in your scripts, and give it a try. cron uses a minimal environment, so the script may not know where to find files.

Code:
#!/bin/bash
# launcher.sh
# activate the cv environment ,then execute
# python script

source $HOME/.profile 
workon cv

python  $HOME/pi_surveillance.py --conf $HOME/conf.json

Last edited by goumba; 07-29-2015 at 03:53 AM.
 
Old 07-29-2015, 04:19 AM   #3
browny redandy
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
How to find the full path of the path

How do one find the full path?
I tried
Code:
$ find "$(cd ..; pwd)" -name "conf.json"
It gives me
Code:
/home/pi/conf.json
The other qn is do I replace /home/pi with $HOME?

Sorry for the noob question. I am new to this.

Thanks!
 
Old 07-29-2015, 04:53 AM   #4
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by browny redandy View Post
How do one find the full path?
I tried
Code:
$ find "$(cd ..; pwd)" -name "conf.json"
It gives me
Code:
/home/pi/conf.json
There you go, you have the full path.

Quote:
The other qn is do I replace /home/pi with $HOME?

BTW,
Code:
find .. -name "conf.json"
would have been fine.

Sorry for the noob question. I am new to this.

Thanks!
$HOME and the tilde ~ are synonymous. I tend to use $HOME as it's a little clearer, doesn't get lost in other text. Either will expand to the user's home directory. in this case, /home/pi.
 
Old 07-29-2015, 05:18 AM   #5
browny redandy
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
One problem solved, the other arises

Thanks for your guidance so far! It worked well.
 
  


Reply



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] bash script runs perfectly when started manually - but not when started by CRON jonasbdk Linux - General 12 08-27-2014 05:54 AM
[SOLVED] Bash script dont works when executed as cronjob postcd Linux - General 10 04-14-2014 11:36 AM
[SOLVED] Writing to file with python script scheduled to run with cronjob rahatb Programming 2 04-03-2014 05:13 AM
[SOLVED] Python TypeError when calling Python program from a Bash script noppeli Programming 2 01-15-2013 08:06 AM
python rsync script runs from shell not from cron beanbox Programming 3 11-18-2009 06:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:26 PM.

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
Open Source Consulting | Domain Registration