How to run my python script when the sunOS is start booting

77 views Asked by At

I copied the script (server.py) to /etc/init.d folder.

chmod 0744 /etc/init.d/server.py
chown root:sys /etc/init.d/server.py 
cd /etc/init.d
ln server.py /etc/rc2.d/Sserver.py
ln server.py /etc/rc0.d/Kserver.py
ls /etc/init.d/*server.py /etc/rc2.d/*server.py /etc/rc0.d/*server.py

(able to see the links created in step 5 and 6)

then I power off the sunOS , and started. But unfortunately I can't see the server.py is running. I checked it using ps -ef.

I wanted to know ,is there anything I missed here or any other configuration steps were missing

1

There are 1 answers

5
pts On BEST ANSWER

After creating the correct symlinks, your script in /etc/init.d will be called with sys.argv[1] == 'start' upon startup and sys.argv[1] == 'stop' upon shutdown. Make sure it works with these values.

To debug this further, write a debug script /etc/init.d/mydebug containing this:

#!/bin/sh
exec >>/tmp/mydebug.log
echo
echo ---
date
id
for ARG in "$@"; do echo "arg: $ARG"; done
echo ENV:
env | sort
echo

, do the chmod, the chown, create the symlinks, reboot, and check the contents of the file /tmp/mydebug.log.


@AndrewHenle has commented that on SunOS the run-control scripts are sourced by /bin/sh. To accommodate this, start your Python script like this (and then follow with Python source code):

#!/bin/sh
""":"; exec python -- "$0" "$@" #"""

Replace python above with /usr/local/bin/python or wherever your Python interpreter is.

This will make it work in both cases: if it's exec()ed and if it's sourced by /bin/sh.


Based on your comment, SunOS is running the mydebug script at startup. So there is a problem with server.py only (not with mydebug).

You mention starting server.py with #!/usr/bin/python. This doesn't work if run-control scripts are sourced by /bin/sh. Instead of this line, please use the 2 lines I've recommended above instead. Don't forget to change python to /usr/local/bin/python if your Python interpreter is there.

As a preparation, run this as root:

chown root:sys /etc/init.d/server.py 
chmod 755 /etc/init.d/server.py

To diagnose this further, run this from the command-line as root:

# Run the following command as root.
cd / && /usr/bin/env -i _AST_FEATURES="UNIVERSE - att" _INIT_PREV_LEVEL=S _INIT_RUN_LEVEL=3 _INIT_RUN_NPREV=0 _INIT_UTS_ISA=i386 _INIT_UTS_MACHINE=i86pc _INIT_UTS_NODENAME=t8a1 _INIT_UTS_PLATFORM=i86pc _INIT_UTS_RELEASE=5.11 _INIT_UTS_SYSNAME=SunOS _INIT_UTS_VERSION=11.4.42.111.0 LANG=en_US.UTF-8 LC_ALL= LC_COLLATE= LC_CTYPE= LC_MESSAGES= LC_MONETARY= LC_NUMERIC= LC_TIME= PATH=/usr/sbin:/usr/bin /bin/sh /etc/init.d/server.py start; echo "Exit code: $?"

The command above is equivalent to what's happening at SunOS system startup. But in here you will get a direct error message, and you can retry it quickly, without having to restart your system.

Success looks like this: the server.py program starts the server in the background and exits successfully (you see Exit code: 0). If you see anything else (e.g. a different exit code, an error message, server.py doesn't exit quickly), then that's an error which you have to fix in your own code (server.py). You may want to ask a separate question about that on StackOverflow.