2

I have a bash file, it works fine when executed from terminal.

#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py

Note: tensorflow_p36 being an in-built conda environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.

If I run this bash script with sudo it doesnt activate the virtual environment and works in default python environment. The python file can run in that virtual environment only.

I have tried all 3 alternatives (rc.local, .conf file, init.d config)here, also tried to use crontab as suggested here. I have also tried using supervisord to add this bash script as a program.

When the program runs from these methods, I always get the same import errors because it is using default python 3 environment which doesn't have the required dependencies.

I am working on Amazon CentOS (Deep learning AMI). Can someone please suggest a method to run this script every time system restarts?

Grimlock
  • 1,033
  • 1
  • 9
  • 23

3 Answers3

1

In the rc.local, instruct root to run it as you:

su --command /path/to/bash/file --login grimlock
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

You can run it from your personal Crontab.

( crontab -l; printf '@reboot /path/to/bash/file\n' ) | crontab -

If you don't have a crontab there will be an error message from crontab -l but it's harmless.

crontab: no crontab for ec2-user

You just need to do this once, and the job will execute as yourself once the system comes up.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I had tried both root Crontab and ec2-user Crontab, but for some reason it just does not work. Does it have any sort of time limit? Like my process takes about 15 minutes to complete and uses 11+ GB of RAM. Not sure if it matters. – Grimlock Mar 08 '18 at 10:44
  • 1
    Nope, `cron` simply kicks off the process, logs the event, and moves on. – tripleee Mar 08 '18 at 10:54
  • I read in /var/log/cron file: Mar 8 09:59:44 ip-xxx-xxx-xxx-xxx CROND[3617]: (ec2-user) CMD (/home/ec2-user/abc/runinenv.sh ), still it did not execute it. – Grimlock Mar 08 '18 at 10:57
  • That means it *did,* unequivocally. It may not have finished successfully but that is not something `cron` monitors or cares about, and not something we can meaningfully discuss without seeing the code or at least some sort of logged output. (Check your mail inbox.) – tripleee Mar 08 '18 at 10:58
0

try to change source by .

. activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py

also check chmod +x your path file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The Anaconda `activate` documentation generally suggests Bash and `source` rather than the `sh` syntax. If you are using Bash, `source` is just an alias for `.` – tripleee Mar 07 '18 at 18:40