Starting python script on Google Cloud Compute instance startup - python

I've been trying to find the best way to start a python script on startup of my cloud compute instance. So far, I haven't gotten it to run. The script does run when manually executed on the instance. I did make the file executable.
This is what I have tried so far:
1)
Add script directly to metadata with key "startup-script". Script starts with:
#! /usr/bin/python3
Followed by the script contents.
Result:
Won't run, doesn't show up in log.
2)
Try to execute local script from metadata with key "startup-script":
#! /usr/bin/bash"
/home/dir/scripts/script.py.
Result:
Won't run, doesn't show up in log.
3)
Point to file located in storage bucket with "startup-script-url".
gs://project.appspot.com/folder/script.py
Result:
Won't run, but shows "Found startup script" in log.
I hope anyone has some insights.

This worked for me:
#! /bin/bash
cat <<EOF > /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
EOF
python3 /var/myScriptStackOverflow.py
The script above is explicit in respect to the paths, but this also works:
#! /usr/bin/python3
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed this way as well...")
Edit the instance, paste the script above in the Custom metadata with the key startup-script:
Reset the instance
ssh inside the instance to check the results:
ls -la /var | grep -i py
-rw-r--r-- 1 root root 119 Aug 3 17:33 myScriptStackOverflow.py
-rw-r--r-- 1 root root 33 Aug 3 17:33 python_was_here_stack_overflow.txt
cat /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
cat /var/python_was_here_stack_overflow.txt
Looks that the script is executed

Related

Why cron job doesn't compile a python file?

Cron doesn't compile my .py file and I can't get an e-mail from Cron to understand why Cron doesn't work. But if I just run the command ~/t/test.sh in terminal everything works fine.
Code in the cron:
SHELL=/bin/bash
MAILTO=mymail#gmail.com
30 0-23 1-31 1-12 0-6 ~/t/test.sh
I ty to run a script test.sh which has the code:
#!/bin/bash
cd /home/alex/t && python3.8 ./test.py
Code in the test.py:
from datetime import datetime
current_time = datetime.now()
f = open("text.txt", 'w+')
f.write("Hello world! Now is {0}\n".format(current_time))
f.close()
I Launched Cron via the sudo crontab-e and crontab-e commands. I Put Cron in the first line MAILTO=mymail#gmail.com.
I Installed yum install mailx. I tried adding the command >/dev/null 2>&1 to Cron, i.e. writing
* * * * * ~/t/test.sh >/dev/null 2>&1
Nothing helps
I Use Ubuntu 18.04
Python3.8.2
Here is what the logs for the grep CRON /var/log/syslog command show:
Apr 30 15:35:01 av CRON[2130]: (alex) CMD (~/t/test.sh)
Apr 30 15:35:01 av CRON[2129]: (alex) MAIL (mailer 68 bytes of output put get status 0x004b from MTA#012)
Has anyone encountered this problem?
Can you help?
your test.sh should be something like this: (needs full path)
#!/bin/bash
/"location where python is installed"/python/bin/python /home/alex/t/test.py
The weakest link in you code is the fact that you invoke the Python 3.8 interpreter without a full path. You invoke bash using a full absolute path, cd to an absolute path, and access your script through a path to the directory into which you cd-ed. Only Python is assuming the existence of some directories in the PATH environment variable.
Try accessing Python 3.8 through the full path to its location.

one python3 script shoud start a second python3 script

i´ve a few problems with my python3 scripts.
an php script start an python3 script:
$comando = 'python3 /var/www/html/tmp/' . $usersession . '-newtenent-vcenter1.py';
shell_exec("/usr/bin/nohup ".$comando." >/dev/null 2>&1 &");
the python3 script write a few strings to an new created text file.
After all thinks are done, the script sould be start the next python3 script:
os.system('python3 /var/www/html/tmp/' + usersession + '-newtenent-cucm1.py')
BUT, python3 start the script "cucm1.py" and close it imeadlety! The script shoud be open an ssh session with paramiko.
The OS is an Ubuntu 18.x. I´ve added the www-data user to the script directory (so all scripts can be executed by the user www-data):
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 /var/www/html/
BUT, when we execute the first python3 script from the linux shell (as root) it work´s fine (the second script working fine).
any idea? THANK YOU!

Run a .sh file with Python 3 Idle

I am trying run a sh file with python 3. My .sh file will work on Terminal. My operating system is Raspbian. I try that code :
import time
import os
import subprocess
# STEP 1
text='sudo somecode'
savefile=open('step1.sh','w')
savefile.write(text)
savefile.close()
time.sleep(2)
shellscript=subprocess.Popen(['./step1.sh'], stdin=subprocess.PIPE)
but it not works...
The is undoubtedly a permissions issue. In order to be able to "directly" execute a file (a la "dot slash" - ./yourfile), the file in question needs the "execute bit" set. Try using ls -l to see the file you've just created with your script. I'll bet the file does not have the execute bit:
$ ls -l ./step.sh
-rw-r--r-- 1 furkan furkan 0 Nov 13 20:51 step.sh
Note the lack of x in that first column. You might chmod to add the execute bit:
$ chmod +x ./step.sh
$ ls -l ./step.sh
-rwxr-xr-x 1 furkan furkan 0 Nov 13 20:51 step.sh
With the execute bit set, you could then use a "dot slash" construct.
However, I doubt you want to execute chmod from within your script, so instead tell your script the actual program you want to run step.sh, namely, sh:
shellscript=subprocess.Popen(['sh', './step1.sh'], stdin=subprocess.PIPE)
Or in the simple case of your example, simply go directly to sudo:
shellscript=subprocess.Popen(['sudo', 'yourexecutable'], stdin=subprocess.PIPE)
Note that if you're being robust, I might consider adding some absolute paths, or ensuring your PATH variable is set. However the key to your problem is the misunderstanding of what 'executable' means.

How to correctly execute a python script on the server?

I'm trying to execute my python script on the server. I use a hosting-company that allows me to do cronjobs. when i try to make the server execute the file i always get this error:
command /homez.208/thomasdewh/www/denshi/denshi.py must be executable
-- 2014-10-13 10:28:02.687788 exitcode: 255
So i found out that i have to use a sheban to tell the server this file can be executed.
So i added:
#!/www/python/python
import zipfile
import os.path
import os
import sys
...rest of script...
I uploaded this to the file and added all the files that are in C:\Users\my-computer\python27\ to the folder /www/python/python on my webserver via FTP.
But this still dont seem to work.. anybody has an idea what i'm doing wrong?
On a unix system, in order to make a script able to run when invoked, there are 2 things to consider:
the shebang #!... at the top of the file, this just tells the shell it is executed from, which interpreter to be used during the execution
ensure that the file has the executable permission for whoever needs to run it: if is is the current user, then chmod u+x , if it has to be for any member of a given group: chmod g+x , if anobody: chmod a+x
When you do: ls -l, you file must probably be like this:
-rw-r--r-- 1 apero staff 0 Oct 14 00:04 script.py
If you set u+x (what you must probably do in your case) it becomes:
-rwxr--r-- 1 apero staff 0 Oct 14 00:04 script.py
if you set g+x it becomes:
-rw-r-xr-- 1 apero staff 0 Oct 14 00:04 script.py
if you set a+x:
-rwxr-xr-x 1 apero staff 0 Oct 14 00:04 script.py
The same can be done for write permissions (w instead of x), and to remove some permission, do the same but with - instead of +.
However, try to never use "a" or "0" as it grants everybody permissions to the file (in read, or write or execute). Btw, whoever can write, can delete, so beware.

Altering my python path: helloworld.py returns command not found—

Massive apologies for this embarrassing question—
I'm using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are laughing at me:
Let me preface with:
$ whereis python
/usr/bin/python
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
(Is this my issue?)
I wrote helloworld.py to /users/charles in vim:
$ vim helloworld.py
#!/usr/bin/python
# Hello World Python Program
print "Hello World!";
When trying to run it from terminal:
$ helloworld.py
-bash: helloworld.py: command not found
When trying to run it from python:
$ python
>>> helloworld.py
Traceback (most recent call last):
File :<stdin>", line 1, in <module>
NameError: name 'helloworld' is not defined
From Dive Into Python (not sure if this is pertinent):
$ python
>>> import sys,os
>>> print 'sys.argv[0] =',sys.argv[0]
sys.argv[0]=
>>> pathname=os.path.dirname(sys.argv[0])
>>> print 'path=',pathname
path=
>>> print 'full path=',os.path.abspath(pathname)
full path= /Users/charles
I'm befuddled! Do I need to alter one of my paths so it finds my script?
I'm absolutely new to programming, I actually just found out that terminal was something you could use.
Thanks!
Let's start with the first error you received. Understanding error messages is important.
-bash: helloworld.py: command not found
This indicates that helloworld.py is not a command that can be executed. To run the file, you then have two options:
Run it using the python interpreter. python helloworld.py
Make the file executable and then run it directly. ./helloworld.py
To make files executable in a *nix environment, you have to change their mode to allow execution. In order to do this, you use the chmod command (man chmod for more info).
chmod +x helloworld.py
This assumes that you are in the directory containing the helloworld.py file. If not, cd there first or use the full path.
The ./ is necessary because it tells the shell to run the file located here, not by looking in $PATH. $PATH is a list of possible executable locations. When you try to run helloworld.py directly, the shell tries to look for it in $PATH. You want to run the local file, so you have to prefix it with ./, which means "from here".
As an aside, note the first line of your python script:
#!/usr/bin/python
This is called a shebang line and tells system to use the /usr/bin/python executable to load the file. Internally, that means that the program loader will be doing /user/bin/python helloworld.py.
Finally, when you called python with no arguments, you were dropped into an interactive Python interpreter session. >>> helloworld.py in this environment is not referencing the file of that name, it's just interpreted as python code. Invalid python code. This is why you get your second error, NameError: name 'helloworld' is not defined.
To turn a Python module or script into a standalone program on a UNIX system you have to do two things:
1.) Make sure you have the "shebang" in the top of your script:
#!/usr/bin/python
2.) Make sure the script file is executable. This is done using the chmod command:
chmod +x /path/to/helloworld.py
/path/to/ being the fully qualified file path to your script. If it's in the current directory, then you can omit the path.
% ls -l
total 0
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rw-r--r-- 1 jathan jathan 0 2011-04-13 15:28 helloworld.py
It's in my current directory, so let's make it executable!
% chmod +x helloworld.py
% ls -l
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rwxr-xr-x 1 jathan jathan 0 2011-04-13 15:28 helloworld.py*
See the "x"s in the permission bits on the left? You've done it! Now we can run it:
% ./helloworld.py
Hello World!
Lastly, never use semicolons as line-endings in Python. It's not required and it's ugly!
Wanted to add my 2 cents: Apart from permissions and path answers above, there is one more situation where you may still face the same error.
In-spite of correct permissions and the shebang header, you may still get the same "Command not found" error if you've originally written the file in Windows and copied it over to Linux. Due to differing line-ending characters, there will be extra '\r' characters on the lines.
This happens because there are non-printable characters in the file. Examing it by doing:
cat -v <filename>:
#!/usr/intel/bin/python^M
The extra "^M" is the problem. Use 'dos2unix' to convert the file and then it'll run fine.
as others said you should chmod +x your file to make it executable and if you don't want to put "./" in your coomand line you should add your current place as system path:
export PATH=$PATH:.
If you're already within python, the syntax to load your script is not helloworld.py :
import helloworld
or
from helloworld import *
you only use the extension .py when you're running python with a script as a command line argument.
No need to apologize, have to start somewhere, and the error messages can be cryptic when you're having basic syntax problems.
Make sure your terminal's current working directory is where your .py file is.
EDITED:
try doing /usr/bin/python helloworld.py on commmand line

Categories

Resources