簡體   English   中英

linux腳本輸出到控制台1

[英]linux script output to console 1

我每周日晚上在Linux PC上使用cron作業運行腳本。 當我以root身份登錄時,我希望顯示最后一次腳本執行的輸出。

這是一個腳本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

其輸出:

starting  service 

要么

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

登錄后如何顯示此輸出?

在您的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

在您的/root/.bash_profile這是當你以root身份登錄執行(假設你的shell是/bin/bash ), cat就將該文件:

cat /root/cronjob_status.log

保存到文件(Afenster的答案)是一個很好的解決方案。

另外,您可以在cron配置文件中設置MAILTO變量。 然后,Cron會將其運行的命令的任何輸出發送到該地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM