Operating System - Linux
1828038 Members
1979 Online
109973 Solutions
New Discussion

Exit from script to a .txt file..It does not work

 
SOLVED
Go to solution
hboo
Frequent Advisor

Exit from script to a .txt file..It does not work

Hello everybody
I am developing a script to verify that a process has being executed and I'm sending the exit to a .txt file , the script for some motive not writing this one in the .txt file Someone will be able to help me with this? Please
Thank you
12 REPLIES 12
spex
Honored Contributor

Re: Exit from script to a .txt file..It does not work

Hi Hayse,

#!/usr/bin/sh
program_to_execute
echo $? >> result.txt
exit

PCS
James R. Ferguson
Acclaimed Contributor

Re: Exit from script to a .txt file..It does not work

Hi:

It would be helpful to post your script. However, in general, all you should need is something like:

# ./myscript; echo $? > ./myscript.returnval

Make sure that you have permission to write into the directory that will hold the file containing the script's return value.

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: Exit from script to a .txt file..It does not work

Hi,
you can check the status of a script with $?

so:
/usr/sbin/fbackup -nAvf /dev/rmt/0m -s -g $GRAPH_DIR/graph >> $LOG_FILE 2>&1
STATUS=$?
if [ $STATUS != 0 ]
then
echo "error has occurred"
fi

hboo
Frequent Advisor

Re: Exit from script to a .txt file..It does not work

Thanks
But
When I execute manually the script, the .txt file is update, the script is in a cron, when he script is executed from cron file, the .txt file is not update
James R. Ferguson
Acclaimed Contributor
Solution

Re: Exit from script to a .txt file..It does not work

Hi (again):

Cron's environment is very sparse. That is, all you are provided for a PATH is:

PATH=/usr/bin:/usr/sbin:.

Hence, unless you explicitly declare the PATH and any other variables you need in your script, then that's all you get! Have a look at the 'crontab(1)' manpages for more information.

One other mis-used technique often seen is to source (read) your profile in the crontab before executing your script. This is a poor choice for a variety of reasons. I suggest that you declare all of the environmental variables you need either in your script or in a file that you source (read) in your script.

Regards!

...JRF...
spex
Honored Contributor

Re: Exit from script to a .txt file..It does not work

Hayse,

If you are comfortable with supplying your script, I'm sure we can point out the exact error.

PCS
hboo
Frequent Advisor

Re: Exit from script to a .txt file..It does not work

Hi, this is the content of my script...
HORA=$(date)
echo $HORA > file.txt
ps -ef | grep -v grep | grep svr > /dev/null 2> /dev/null
if [ "$?" -eq 0 ]
then
echo "================" >> file.txt
echo " Executing " >> file.txt
echo "===================" >> file.txt

else
echo "===============" >> file.txt
echo "NOT Executing " >> file.txt
echo "===============" >> file.txt
su - user -c "/home/user/start_service.sh"

fi
exit
hboo
Frequent Advisor

Re: Exit from script to a .txt file..It does not work

Ahhh
My script first line:
#!/bin/sh
Chris Hendrick_2
Occasional Advisor

Re: Exit from script to a .txt file..It does not work

I believe a line like this at the start of the script (after setting the variables) will work:

exec > $LOG_FILE 2>&1

That will write std out and std err to $LOG_FILE

Hope that helps.
hboo
Frequent Advisor

Re: Exit from script to a .txt file..It does not work

Thanks all of you...
I solved the problem
A little detail with PATH

Bye and Thanks
; )
hboo
Frequent Advisor

Re: Exit from script to a .txt file..It does not work

Regards
Dennis Handly
Acclaimed Contributor

Re: Exit from script to a .txt file..It does not work

>ps -ef | grep -v grep | grep svr > /dev/null 2> /dev/null

There is no need to redirect the grep output to /dev/null if you use grep -q.