0

I'm running a script using cron job every Sunday night on my Linux PC. When I log in as root I would like the output of this last script execution to be displayed.

Here is a script 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?

2 Answers 2

2

In your cron job, save the output to some file:

#!/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:

cat /root/cronjob_status.log
Sign up to request clarification or add additional context in comments.

Comments

1

Saving to a file (afenster's answer) is a good solution.

Alternatively, you can set the MAILTO variable in the cron configuration file. Cron will then send any output from the command it runs to that address.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.