2

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

pts
  • 80,836
  • 20
  • 110
  • 183
  • 1
    SunOS! As far as I remember, startup scripts used to have a 2-digit number following the `S` in their names, e.g. `S90nfsd`. Also, you'll need a proper Python shebang as the first line of your `server.py` if it is even possible to use a non-bash script. – Mark Setchell Jul 24 '23 at 09:58
  • I modified as, /etc/init.d/server.py /etc/rc0.d/K90server.py /etc/rc2.d/S90server.py and also added Python shebang as #!/usr/bin/env python3 but there is no improvement, can't see the server.py running after reboot sunOS – Sarath Govind Jul 24 '23 at 11:01
  • 1
    I can't remember that far back, but try looking at some other startup scripts. I think they get called with a parameter of 'start' or 'stop' so you may need to wrap your Python inside a `bash` script to handle that... or hopefully someone else can remember... – Mark Setchell Jul 24 '23 at 11:13
  • Use cron for start up when booting. – toyota Supra Jul 24 '23 at 14:15
  • 1
    @toyotaSupra That's what init.d scripts are for. Abusing cron like that is a Linux thing. – Andrew Henle Jul 24 '23 at 16:07
  • 1
    @MarkSetchell I'm pretty sure Solaris run-control scripts are *sourced* by `/bin/sh`, and, unlike Linux, there's no conflation of `sh` and `bash` on Solaris. And the name of the script here sure looks like a Python script - which, as you imply, probably won't work. – Andrew Henle Jul 24 '23 at 16:11

1 Answers1

3

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.

pts
  • 80,836
  • 20
  • 110
  • 183
  • Note, though, that the OP's script is `/etc/init.d/server.py`, which is likely a Python script. Which probably won't work when sourced by `/bin/sh`. – Andrew Henle Jul 24 '23 at 16:12
  • 1
    @AndrewHenle: I've added a workaround for run-control scripts being sourced by `/bin/sh`. – pts Jul 24 '23 at 18:01
  • @pts , still not able to see script is running when sunOS booting. Tuesday, July 25, 2023 at 9:31:01 AM CEST uid=0(root) gid=0(root) arg: start ENV: _=*1108*/usr/bin/env _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 A__z="*SHLVL LANG=en_US.UTF-8 LC_ALL= LC_COLLATE= LC_CTYPE= LC_MESSAGES= LC_MONETARY= LC_NUMERIC= LC_TIME= PATH=/usr/sbin:/usr/bin PWD=/ SHLVL=1 – Sarath Govind Jul 25 '23 at 07:53
  • @pts also added a line like this inside /etc/init.d/server.py #!/usr/bin/python permission are like this -rwx-wx-wx 3 root sys 45 Jul 24 13:34 server.py -rwxr--r-- 3 root sys 125 Jul 25 08:58 mydebug links are, /etc/init.d/server.py /etc/rc0.d/K05SERVER /etc/rc2.d/S05SERVER do this links also have the same source code as server.py? – Sarath Govind Jul 25 '23 at 07:55
  • Thank you for posting to output of *mydebug*. Based on that I've extended my answer. – pts Jul 25 '23 at 08:41
  • @pts, Thank you so much. It worked!!!! – Sarath Govind Jul 25 '23 at 09:32
  • @SarathGovind: If this answer works for you, please accept it by clicking on the tick. More info here: https://stackoverflow.com/help/someone-answers – pts Jul 25 '23 at 11:56