简体   繁体   English

linux脚本输出到控制台1

[英]linux script output to console 1

I'm running a script using cron job every Sunday night on my Linux PC. 我每周日晚上在Linux PC上使用cron作业运行脚本。 When I log in as root I would like the output of this last script execution to be displayed. 当我以root身份登录时,我希望显示最后一次脚本执行的输出。

Here is a script msg.sh : 这是一个脚本msg.sh

#!/bin/bash
if [ -e /user/script/$start.sh ]
then 
   echo " starting  service "
else
   echo " service not started " 
   echo " Please check the start.sh file or manually start the service "
fi
exit 0

Its output: 其输出:

starting  service 

or 要么

service not started
Please check the start.sh file or manually start the service

How can I make this output appear when I log in? 登录后如何显示此输出?

In your cron job, save the output to some file: 在您的cron作业中,将输出保存到一些文件中:

#!/bin/bash

# drop the contents of a log file and create a new one
LOGFILE=/root/cronjob_status.log
date > $LOGFILE

if [ -e /user/script/$start.sh ]
then 
   echo " starting  service " >> $LOGFILE
else
   echo " service not started " >> $LOGFILE
   echo " Please check the start.sh file or manuly start the service " >> $LOGFILE
fi
exit 0

In your /root/.bash_profile which is executed when you log in as root (assuming that your shell is /bin/bash ), cat that file: 在您的/root/.bash_profile这是当你以root身份登录执行(假设你的shell是/bin/bash ), cat就将该文件:

cat /root/cronjob_status.log

Saving to a file (afenster's answer) is a good solution. 保存到文件(Afenster的答案)是一个很好的解决方案。

Alternatively, you can set the MAILTO variable in the cron configuration file. 另外,您可以在cron配置文件中设置MAILTO变量。 Cron will then send any output from the command it runs to that address. 然后,Cron会将其运行的命令的任何输出发送到该地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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