Exit status

Create this script in IDLE and save it as exitstatus.py.

import sys

print("About to return exit status 123.")
sys.exit(123)

On macOS,

cd /Users/myname/python
python3 exitstatus.py
About to return exit status 123.
echo $?
123

On PC,

cd C:\Users\Myname\python
py.exe exitstatus.py
About to return exit status 123.
echo %errorlevel%
123

Python scripts

These Python programs do not call the print function and therefore they produce no output. But they do produce an exit status number.

  1. ibm.py gets the current price of IBM stock from this web page.
  2. temperature.py gets the current temperature in New York from this file in JSON format.
  3. """
    strand.py
    
    Exit with exit status 0 if the Strand bookstore is currently open, 1 if closed.
    """
    
    import sys
    import datetime
    
    d = datetime.datetime.now()
    
    #number of hours after midnight
    currentHour = d.hour + (d.minute + (d.second + d.microsecond / 1_000_000) / 60) / 60
    
    if currentHour >= 12 + 10.5: #10:30 p.m.
        sys.exit(1)       #Strand has closed for the night.
    
    if d.weekday() == 6:  #Sunday
        openingHour = 11  #11:00 a.m.
    else:
        openingHour = 9.5 # 9:30 a.m.
    
    if openingHour <= currentHour:
        sys.exit(0)   #Strand is already open.
    else:
        sys.exit(1)   #Stand hasn't opened up yet this morning.
    

Set the time zone on AWS Linux

See
man 5 localtime

$ date
Mon Feb 10 11:21:45 UTC 2020

$ cd /etc
$ pwd
/etc

$ ls -l localtime
lrwxrwxrwx 1 root root 25 Feb  7 12:28 localtime -> ../usr/share/zoneinfo/UTC

$ sudo ln -sf ../usr/share/zoneinfo/US/Eastern localtime

$ ls -l localtime
lrwxrwxrwx 1 root root 32 Feb 10 06:23 localtime -> ../usr/share/zoneinfo/US/Eastern

$ date
Mon Feb 10 06:23:32 EST 2020

$ cd /usr/share/zoneinfo/US
$ pwd
/usr/share/zoneinfo/US

$ ls -l Eastern
-rw-r--r-- 3 root root 3545 Oct 23 16:47 Eastern

$ file Eastern
Eastern: timezone data, version 2, 5 gmt time flags, 5 std time flags, no leap seconds, 236 transition times, 5 abbreviation chars

$ od -c Eastern | head -4
0000000   T   Z   i   f   2  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0  \0  \0 005  \0  \0  \0 005  \0  \0  \0  \0
0000040  \0  \0  \0 354  \0  \0  \0 005  \0  \0  \0 024 200  \0  \0  \0
0000060 236 246 036   p 237 272 353   ` 240 206  \0   p 241 232 315   `

Install certificates on macOS

SSL stands for Secure Sockets Layer. CA stands for Certification Authority. TLS stands for Transport Layer Security. If the above Python script gives you the following message on macOS,

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>
run the command
Applications → Python 3.8 → Install Certificates.command
The command will install the certifi package and create a text file named cacert.pem. CA stands for Certification Authority.

pip3 list
certifi (2019.9.11)

pip3 show certifi
Name: certifi
Version: 2019.9.11
Summary: Python package for providing Mozilla's CA Bundle.
Home-page: https://certifi.io/
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: MPL-2.0
Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
Requires:
Required-by: requests

cd /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

ls -ld certifi*
drwxr-xr-x  7 myname  mygroup  224 Oct 15 22:49 certifi
drwxr-xr-x  9 myname  mygroup  288 Oct 15 22:49 certifi-2019.9.11.dist-info

The ~/.bash_profile file on macOS and Linux

When you open a Terminal window on macOS, or bash shell window on Linux, the commands in the file ~/.bash_profile are automatically executed. I put the following commands into the ~/.bash_profile on my Mac.

if python3 /Users/myname/python/ibm.py
then
	#ibm.py produced exit status 0.
	echo The price of IBM is at least 140.
else
	#ibm.py did not produce exit status 0.
	echo The price of IBM is less than 140.
	mail nervous@investor.com < formletter.txt
fi

Your Python script should produce an exit status number because the thing that runs the script might not be a human being. It might be another program (e.g., ~/.bash_profile), and that other program might expect to receive an exit status from your script.

Things to try

  1. Change
    if price >= 140.00:
        sys.exit(0)   #success
    else:
        sys.exit(1)   #failure
    
    to the following. See Conditional expressions.
    sys.exit(0 if price >= 140.00 else 1)