我知道我知道。这些帖子一直出现。我发誓,我的机器/代码是大多数人试图完成的任务/他们在尝试设置这些看似简单的脚本时遇到的问题的一个例外。
我在我的 Raspberry Pi(运行 Raspbian OS)上有一个文件IPdetermination.rb
,它基本上使用rest-client
ruby 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
但也不起作用。