简体   繁体   中英

How to flush all ssh server data before the server shutdown?

I have an embedded Linux OS control box that runs an application, which is started as a systemd service. The application monitors the state of a switch.

At the same time, I am logged in the control box via an ssh client shell over Ethernet from a linux PC, monitoring the systemd journalctl output for any logs by the application.

When the switch state is found to be OFF, the application logs the event with the systemd journaled service by printing a message. Consequently, the message gets logged by the systemd journaled service and saved on a non-volatile file.

After printing the message the application executes system("poweroff") .

Consequently, the ssh session is terminated quickly enough without receiving the message printed to the journalctl output at the ssh client side. At the same time the message is successfully logged by the systemd journaled service and saved on the non-volatile file.

With this in mind, I am wondering if there was a specific way to postpone the execution of the system("poweroff") command until all the journalctl log data is transferred/flushed out of the ssh output buffer . And by a specific I mean not an arbitrary sleep() call before the system("poweroff") .

如何在关机脚本中添加一行以回显非易失性文件的tail -n1输出呢?

It is not clear why it should be guaranteed that the message is seen on the other end of the SSH session, and even if we accept it must be guaranteed, I still don't understand why a few seconds delay of the shutdown is critical (which could be achieved with sleep() ), but you could take another approach

If the one or at least one SSH connection is always open to the server, you could initiate the shutdown from SSH session when the log message is written into the file.

tail -f path/to/file |  sed '/^regex pattern$/ q' && command_to_execute_after_match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM