将执行的ssh脚本时间戳记录到服务器上的文件中

时间:2023-02-05 21:59:31

I have a script that executes some commands remotely, every time it runs it appends the time of execution to a log. I thought I was doing it right but then realized the script was sending the time/date of my machine, not using the server time/date in the log.

我有一个脚本,它远程执行一些命令,每次运行时都将执行时间附加到日志中。我以为我做得对,但后来意识到脚本发送的是我的机器的时间/日期,而不是使用日志中的服务器时间/日期。

This is the command I'm using right now:

这是我现在使用的命令:

ssh root@myserver "echo \"*** Log info of $(date -u) ***\" >> /var/log/app.log"

I tried using single quotes but it does not work at all the script shows an error.

我尝试使用单引号,但它根本不起作用,脚本显示错误。

Is there a better way to log that time to a file on the server?

是否有更好的方法将日志记录到服务器上的文件中?

1 个解决方案

#1


3  

Enclose the full command inside single quotes like this:

在单引号中包含完整的命令,如下所示:

ssh root@myserver 'echo "*** Log info of $(date -u) ***" >> /var/log/app.log'

#1


3  

Enclose the full command inside single quotes like this:

在单引号中包含完整的命令,如下所示:

ssh root@myserver 'echo "*** Log info of $(date -u) ***" >> /var/log/app.log'