0

我知道我知道。这些帖子一直出现。我发誓,我的机器/代码是大多数人试图完成的任务/他们在尝试设置这些看似简单的脚本时遇到的问题的一个例外。

我在我的 Raspberry Pi(运行 Raspbian OS)上有一个文件IPdetermination.rb,它基本上使用rest-clientruby​​ gem 来执行带有 JSON 的 http POST。这是代码:

#sends a message to slack using incoming-webhook that identifies that
#host machine's name and ip address.
require 'rest-client'
address = Socket.ip_address_list.detect {|x| x.ipv4_private?}.ip_address
name = Socket.gethostname
if name.include? '.' then name = name.slice(0..name.index('.') - 1) end
payload = {text: "*Device:* `#{name}`\n *IP:* `#{address}`"}.to_json
RestClient.post 'https://hooks.slack.com/services/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', payload

请注意,这确实有效。所以,事实上,这个bash脚本,它存储在/etc/init.d

#! /bin/bash
# /etc/init.d/ip_addr

### BEGIN INIT INFO
# Provides:          ip_addr
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ip address locator
# Description:       sends hostname's ip address on private slack channel
### END INIT INFO
exec ruby ~/Documents/coding/ruby/IPdetermination.rb
exit 0

它们都在手动执行时工作,成功在 Slack 上发布消息。请注意,我已将 LSB 注释附加到ip_addr脚本并配置文件以便运行ls -l返回-rwxr-xr-x 1 root root 413 Dec 30 03:39 ip_addr. 运行chkconfig --list正确显示ip_addr 0:off 1:off 2:on 3:on 4:on 5:on 6:off

然而

它不起作用!重新启动系统似乎不会运行该脚本。我唯一的理论可能是重新启动后 POST 可能出现故障,但我无法确定这是否是问题的根源。我该怎么办?

编辑:将Required-Start:and更改Required-Stop:为还包括引导设施$network$named但也不起作用。

4

2 回答 2

1

/etc/init.d/ 下的 afaik 脚本在重新启动时不会执行。如果您想在进入特定运行级别时在启动时运行脚本,则至少需要在运行级别目录中设置符号链接,并/etc/rc*.d/以 an 为前缀。S如果以它为前缀,K则表示类似于 kill ,因此它不会在启动时执行或在关机时被杀死。附加到这些主要前缀的数值允许您定义脚本在启动时运行的顺序。

因此,如果您想ascript在进入运行级别 2 时在启动时运行,您需要执行以下操作:

$ ln -s /etc/init.d/ascript /etc/rc2.d/S01ascript

这将导致脚本在进入运行级别 2 时首先运行。任何更新机制都喜欢update-rc.dsystemctl enable ...只会设置此类链接以使脚本在启动时可用/调用。

希望这可以帮助。问候

于 2015-12-30T14:44:50.997 回答
0

好的,终于找到问题了,感谢@tasasaki这样的用户。发生的事情是我在其中添加了一个/etc/rc.local名为我的带有完整路径名的 bash 脚本的行,以及编辑 bash 脚本以具有完整路径名。另外rc.localexit 0exit 0. 向上移动它可能也有帮助。如果有人发现这个,希望它有帮助!

于 2016-01-02T04:22:26.200 回答