I am running a command which was installed as a script of a python package i created.
I have a cronned_job_shell_script.sh file:
touch a.txt
my_script_command
where my_scrip_command was installed using pip install my_py_package.tar.gz
The cron line is:
0 * * * * cronned_job_shell_script.sh
If I run cronned_job_shell_script.sh from shell it works fine, running the python script also.
Even trying env -i /bin/bash --noprofile --norc first and then running the script works.
The problem is, when scheduled by cron, the file a.txt is touched but the script does not seem to run.
The problem is that you're putting this in the system crontab, which means it's being run by root, and root has a different PATH variable than your user. So, testing it in the shell (as you, not root) doesn't actually test the same thing.
The easiest solution is to just use the absolute path to the script—/opt/mystuff/bin/my_script_command instead of just my_script_command.
Related
I created a python script ExeMain.py that use virtualenv for dependence. So to launch this script using .desktop I made this command
Exec=sh -c 'source ~/PycharmProjects/ProBlog/venv/bin/activate; python ~/PycharmProjects/ProBlog/ExeMain.py;'
Which resulted with this error "sh: 1: source: not found" and followed by "ImportError"
Without the source command in .desktop file with all necessary modules installed in direct python environment it works fine (in other Linux machine). As it seams not to be a good practice i tried using venv. And as of the errors i could see than the import error is because source command is not executed.
When i launch the .desktop file it should execute the ExeMain.py file.
Standard versions of sh does not have the source command. Perhaps use /bin/bash instead? So it'd be written something along the lines of this:
Exec=/bin/bash -c 'source /home/username/PycharmProjects/ProBlog/venv/bin/activate && /home/username/PycharmProjects/ProBlog/venv/bin/python /home/username/PycharmProjects/ProBlog/ExeMain.py'
I also suggest three other changes: 1) the && operator so that it won't try to run python code until the activate is finished, 2) replace the ~ tilde with the full path in case the desktop environment doesn't properly expand it, and 3) put in the full path to the venv python so that it doesn't default to the system python. Some of these may not be necessary (depending on the system setup), but it doesn't hurt to be careful.
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?
In the rc.local, instruct root to run it as you:
su --command /path/to/bash/file --login grimlock
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.
try to change source by .
. activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py
also check chmod +x your path file.
I tried to make a cron job on crontab which runs a python script on AWS ec2. My python script includes a module that is only available for python3.
Using the following command I changed the ec2 default python interpreter from python2.7 to python3.4
Source /home/ec-2user/venv/python34/bin/activate
and then using pip install, I installed the required module for python3.4. So now the default interpreter is python3.4 and when I run the script on ec2-user directory using the following command:
python test.py
the program runs without any problem (so I am sure the module is installed correctly).
But when I assign python file to a cronjob
* * * * * python test.py
It does not work. Checking the mail, the error is:
“No module found named “xxxxx” “
But as I said it worked fine outside of the cron.
I was wondering if you can help me with this problem. I appreciate your time and information.
You have to make a shell script which will do the steps of changing to script directory, activating virtual environment and then running it.
Example:
#!/bin/bash
cd $YOUR_DIR
. venv/bin/activate
python3.4 test.py
Then you call this script in cron with
/bin/bash /.../script.sh
What you could do additionally is
chmod +x test.py
and add/update first line to:
#!/usr/bin/env python3.4
This way you can just run Python script with ./test.py
Create file as 'user_cron.sh'
#!/bin/bash
cd '/root/my_new_project_python'
. my_project_venv/bin/activate
python3 main.py
set the cron using crontab -e
#!/usr/bin/env python
I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.py as long as I have the above line at the top of the script. Is this correct?
It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.
Universal running of Python scripts
You can pretty much universally run without the shebang (#!) with
python myscript.py
Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):
python -m myscript
from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).
Shebangs (#!) are a Unix thing.
The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.
You can usually default to whatever python is available on your system path with:
#!/usr/bin/env python
Assuming you're on a Unix, you might try other locations for your python setup, like:
#!/usr/bin/python
Muddling through
You can see what python you're currently using by using the unix which command, so if you want to see where your python is coming from, use this command:
which python
or on Windows (cygwin probably can run the shebang):
where python
On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod
chmod +x myscript.py
(chmod also may apply to Cygwin in Windows)
If you're not running as root, you may require sudo, and that would be
sudo chmod +x myscript.py
And then attempt to run (within the same directory) with
./myscript.py
make the file executable
sudo chmod +x /path/to/file.py
and then from the same directory as file.py:
./file.py
I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.
I have a basic script on the filesystem called Standalone.py:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.
How do I run such scripts?
Thanks for any help.
update
I changed the permissions of Standalone.py to 755. Then I ran the command:
$ ./Standalone.py
and received the message:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran
$ ./Standalone.py
I received the message
-bash: ./Standalone.py: Permission denied
Is there something I'm missing?
You need to make the script executable using
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use
./Standalone.py
to tell the shell that the script is in the current directory.
Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.
Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. So you either have to do a chmod +x $file or a chmod 0740 $file. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/.
To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. You can display it with echo $PATH. Those are the directories that are searched for script to execute. So you simply need to give your script the executable right and put it in one of the directory given by your PATH.
Can you check if /usr/bin/python or /usr/bin/python3.2 exists
Execute below comamnd:
which python3.2
and then use the resulting path on top of you script.