Link to home
Start Free TrialLog in
Avatar of theoradically
theoradically

asked on

simple Python script - one completes, run the other, notify

The environment is

$uname -m
x86_64

$cat /etc/*release
skip
VERSION="16.04.4 LTS (Xenial Xerus)"
VERSION_ID="16.04"
UBUNTU_CODENAME=xenial

$python --version Python 2.7.12

I have 2 python scripts one purges a sql database the other dynamically updates the database

script1.py takes a long time to complete. I need help in writing a new python script3.py to run

script1.py

make sure it completes, successfully, and then run

script2.py

and log and email status.

Your help is appreciated!!!
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

I assume that your scripts are executed as-is.
The trick is to import them into script3.
Upon import, the parts in the main body sections will be executed. Therefore, we will attempt to wrap the imports in try/catch.

import smtplib, ssl

try:
    import script1
except:
    print "script1 failed"
    # handle your errors here
    quit()
try:
    import script2
    status = "script2 succeeded"
except:
    print "script2 failed"
    # handle your errors here
    status =  "script2 failed"

# Now send an email. 
smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = "me@gmail.com"
password = "xxxxxpasswordxxxxxx"

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    receiver_email = "your@email.com"
    message = """\
    Subject: {}

    Seems that {} """.format(status, status)
    server.sendmail(sender_email, receiver_email, message)
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()

Open in new window

Avatar of pepr
pepr

I suggest to wrap the body of the scripts 1 and 2 to the functions. Then script3 can import the scripts as modules and to call the functions explicitly:

script1.py
#!/usr/bin/env python2

def main():
    print 'This is the original body of the script 1.'
    
if __name__ == '__main__':
    main()  # This will be executed when you launch the file as a script,
            # but you cannot get here when you import the file as a module.

Open in new window


script2.py
#!/usr/bin/env python2

def main():
    print 'This is the original body of the script 2.'

if __name__ == '__main__':
    main()

Open in new window


script3.py
#!/usr/bin/env python2

import script1
import script2

# You do not need the following "if" but it is a good practice if the script
# gets less simple -- when you can imagine it can be used as a module.
if __name__ == '__main__':
    script1.main()
    script2.main()

# Notice that the functions need not be named "main". Pick another if you think
# there is a better name.

Open in new window


You should observe the output like (here launched in Windows):
D:\__Python\theoradically\ee29153226>py script1.py
This is the original body of the script 1.

D:\__Python\theoradically\ee29153226>py script2.py
This is the original body of the script 2.

D:\__Python\theoradically\ee29153226>py script3.py
This is the original body of the script 1.
This is the original body of the script 2.

Open in new window

Avatar of theoradically

ASKER

Shalom, thank you for your response


This script

import smtplib, ssl
import pdb; pdb.set_trace()

try:
    import fresh_connections.py
except:
    print "script1 failed"
    # handle your errors here
    quit()

try:
    import fresh_guac.py
    status = "script2 succeeded"
except:
    print "script2 failed"
    # handle your errors here
    status =  "script2 failed"

finally:
    server.quit()


When I run it, I get this

/etc/guacamole$ python holy_guac3.py
script1 failed

If I run it with debug, I get this
There are no self. declarations in script1 and 2


(Pdb) Traceback (most recent call last):
  File "holy_guac3.py", line 6, in <module>
    try:
  File "holy_guac3.py", line 6, in <module>
    try:
  File "/usr/lib/python2.7/bdb.py", line 49, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python2.7/bdb.py", line 67, in dispatch_line
    self.user_line(frame)
  File "/usr/lib/python2.7/pdb.py", line 158, in user_line
    self.interaction(frame, None)
  File "/usr/lib/python2.7/pdb.py", line 210, in interaction
    self.cmdloop()
  File "/usr/lib/python2.7/cmd.py", line 130, in cmdloop
    line = raw_input(self.prompt)
Thank you Pepr, for your response

I do want some kind of functionality to verify that fresh_connections, completes successfully

Here are my 3 scripts

-rwxr-xr-x 1 root root 4186 Jul 25 22:43 ./fresh_connections.py
-rwxr-xr-x 1 root root 12971 Jul 10 18:00 ./fresh_guac.py
-rwxr-xr-x 1 root root 128 Jul 31 00:42 ./script3.py


When I run script 3, I get

/etc/guacamole$ python script3.py
Traceback (most recent call last):
  File "script3.py", line 1, in <module>
    import fresh_connections.py
ImportError: No module named py


Both of the .py scripts already have a

def main():
 
*code*

if __name__ == "__main__":
  main()




Here is script3.py


import fresh_connections.py
import fresh_guac.py

if __name__ == '__main__':
    fresh_connections.main()
    fresh_guac.main()
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial