在SunOS启动时如何运行我的Python脚本

huangapple go评论57阅读模式
英文:

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

问题

我将脚本(server.py)复制到了/etc/init.d文件夹。

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

(能够看到在步骤5和6中创建的链接)

然后我关闭了SunOS,并重新启动。但不幸的是,我看不到server.py正在运行。我使用ps -ef命令检查了它。

我想知道,我是否漏掉了什么或是否缺少其他配置步骤?

英文:

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

得分: 3

在创建正确的符号链接之后,位于 /etc/init.d 中的您的脚本在启动时将以 sys.argv[1] == 'start' 被调用,在关闭时将以 sys.argv[1] == 'stop' 被调用。确保它能够处理这些值。

要进一步调试,请编写一个名为 /etc/init.d/mydebug 的调试脚本,其中包含以下内容:

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

执行 chmodchown,创建符号链接,重新启动,并检查文件 /tmp/mydebug.log 的内容。


@AndrewHenle 提到在 SunOS 上,运行控制脚本是由 /bin/sh 执行的。为了适应这一点,请像这样启动您的Python脚本(然后跟上Python源代码):

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

请将上面的 python 替换为您的Python解释器所在的路径,比如 /usr/local/bin/python

这将使它在两种情况下都能工作:如果它被 exec(),以及如果它被 /bin/sh 引用。


根据您的评论,SunOS 在启动时运行了 mydebug 脚本。因此,只有 server.py(而不是 mydebug)存在问题。

您提到使用 #!/usr/bin/python 启动 server.py。如果运行控制脚本是由 /bin/sh 引用的话,这行不起作用。请改用我上面推荐的两行。别忘了将 python 更改为 /usr/local/bin/python,如果您的Python解释器在那里的话。

作为准备工作,请以 root 身份运行以下命令:

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

要进一步诊断,请以 root 身份从命令行运行以下命令:

# 以 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: $?"

上述命令相当于在 SunOS 系统启动时发生的事情。但在这里,您将会得到一个直接的错误消息,并且可以快速重试,无需重新启动系统。

成功的情况下会是这样的:server.py 程序在后台启动服务器并成功退出(您会看到 Exit code: 0)。如果您看到其他任何内容(例如不同的退出代码、错误消息、server.py 退出不及时等),那就是一个错误,您需要在自己的代码(server.py)中解决。您可能想在 StackOverflow 上提一个关于此的单独问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月24日 17:51:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定