如何检测服务器启动何时完成?

如何检测服务器启动何时完成?

我可以在内核完成启动之前建立与服务器的 SSH 连接:

...
Nov 19 08:48:39 myServer kernel: [   11.305033] aufs 3.16-20140908
Nov 19 08:48:39 myServer kernel: [   11.389463] Bridge firewalling registered
Nov 19 08:48:39 myServer kernel: [   11.410197] nf_conntrack version 0.5.0 (4753 buckets, 19012 max)
Nov 19 08:48:39 myServer kernel: [   11.477171] ip_tables: (C) 2000-2006 Netfilter Core Team
Nov 19 08:48:39 myServer kernel: [   11.665557] IPv6: ADDRCONF(NETDEV_UP): docker0: link is not ready
>> I can already connect through SSH around here
Nov 19 08:48:42 myServer kernel: [   14.346036] aufs au_opts_verify:1570:docker[2275]: dirperm1 breaks the protection by the permission bits on the lower branch
Nov 19 08:48:42 myServer kernel: [   14.500609] aufs au_opts_verify:1570:docker[2301]: dirperm1 breaks the protection by the permission bits on the lower branch
Nov 19 08:48:42 myServer kernel: [   14.527922] aufs au_opts_verify:1570:docker[2301]: dirperm1 breaks the protection by the permission bits on the lower branch
Nov 19 08:48:42 myServer kernel: [   14.570338] aufs au_opts_verify:1570:docker[2275]: dirperm1 breaks the protection by the permission bits on the lower branch
Nov 19 08:48:42 myServer kernel: [   14.601081] device vethbdfcd80 entered promiscuous mode
Nov 19 08:48:42 myServer kernel: [   14.613296] IPv6: ADDRCONF(NETDEV_UP): vethbdfcd80: link is not ready
Nov 19 08:48:42 myServer kernel: [   14.718809] IPv6: ADDRCONF(NETDEV_CHANGE): vethbdfcd80: link becomes ready
Nov 19 08:48:42 myServer kernel: [   14.725877] docker0: port 1(vethbdfcd80) entered forwarding state
Nov 19 08:48:42 myServer kernel: [   14.732093] docker0: port 1(vethbdfcd80) entered forwarding state
Nov 19 08:48:42 myServer kernel: [   14.740035] IPv6: ADDRCONF(NETDEV_CHANGE): docker0: link becomes ready
Nov 19 08:48:43 myServer kernel: [   15.488079] IPv6: eth0: IPv6 duplicate address fe80::42:acff:fe11:1 detected!
Nov 19 08:48:57 myServer kernel: [   29.760031] docker0: port 1(vethbdfcd80) entered forwarding state
Nov 19 08:49:29 myServer kernel: [   61.596905] ...
Nov 19 08:49:44 myServer kernel: [   76.786652] ...
Nov 19 08:49:44 myServer kernel: [   76.805961] ...
Nov 19 08:49:44 myServer kernel: [   76.825477] ...
>> I want to execute something here when the server finished startup

我如何在启动结束时执行某些操作?我现在需要配置/启动所有内容。

答案1

最简单的方法是在你的 cron 中使用@reboot。

编辑你的 crontab

crontab -e

并添加以下内容:

@reboot /path/to/your/program

这将在 crontab 守护进程启动时执行

答案2

您可以制作自己的systemd服务。
systemd.unit — 单元配置

你需要AfterBefore选项:

以空格分隔的单元名称列表。配置单元之间的顺序依赖关系。如果单元 foo.service 包含设置 Before=bar.service 并且两个单元都在启动,则 bar.service 的启动将延迟到 foo.service 启动后。

这意味着只要网络(或任何你想要的)启动,systemd你的服务就会启动。我认为这就是你想要的。

我为你写了一个示例服务(以 NetworkManager 服务为参考)

[Unit]
Description=SSH connection
Wants=network.target
After=network.target # as soon as network is up systemd will start your service

[Service]
Type=simple
ExecStart=/path/to/script
Restart=on-failure

[Install]
WantedBy=multi-user.target # This will insure that your ssh connection is done before you see login screen

这是样本服务设置,在部署之前你可能需要阅读以下资源

更多的:systemd.unitsystemd.service

相关内容