LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to create log file in shell script (http://www.linuxquestions.org/questions/linux-server-73/how-to-create-log-file-in-shell-script-879311/)

ramecare 05-07-2011 04:13 AM

How to create log file in shell script
 
Hi all,

I tried to create logfile for the below shell program but i was not able to get log file,can anyone help me for the below program to create a log file for the shellscript.

#/bin/bash
mount -t cifs -o user="$1",password="$2" "$3"/"$1" "$4"
echo " Mounted Successfull " >> /var/log/kannan
cd "$4""$5"
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
mysqldump -u"$6" -p"$7" "$8" > $date.sql
echo " Backup taken Successfull " >> /var/log/kannan
rm -rf $date.sql

Thanks,

acid_kewpie 05-07-2011 04:28 AM

if you have permissions to /var/log as the executing user then that will work just fine. You might also like to use the "logger" tool to send messages into syslog more nicely.

EricTRA 05-07-2011 04:28 AM

Hello,

Have a look at the /var/log directory and which user runs your script. That might shed some light on why the log doesn't get created. Also you're writing to the log that your processes have completed successfully, even when they might fail so there's no real use since you cannot confide in the log file. You should put some test in place to check if the commands exited with a success code and change the message accordingly.

Kind regards,

Eric

ramecare 05-07-2011 04:46 AM

Iam running the program from root user only,i have given permission for tat /var/log and i have given full permission for the logfile kannan,but also i was not able to get the logfile,can anyone give suggesion on this.

Thanks,

EricTRA 05-07-2011 05:13 AM

Hello,

If your script runs as root than it should work. Also if running as root then what's the need of setting the permissions to kannan? Try running in a terminal:
Code:

echo "test">>/var/log/kannan
and see if the file gets created and that it has the word test in it.

How does your script run? From a crontab? Or do you only launch it manually?

Kind regards,

Eric

ramecare 05-07-2011 06:13 AM

#/bin/bash
mount -t cifs -o user="$1",password="$2" "$3"/"$1" "$4"
echo "test">>/var/log/kannan
cd "$4""$5"
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
mysqldump -u"$6" -p"$7" "$8" > $date.sql
tar -zcvf $date.sql.tgz $date.sql
rm -rf $date.sql

When is run echo "test">>/var/log/kannan this alone in its working but when i user this command echo "test">>/var/log/kannan with my shell program i was not able to get log file,could any one help me wat whould be the problem,iam running my shell program manually only.

Thanks,

EricTRA 05-07-2011 06:15 AM

Hi,

Do the other commands in your script execute as expected?

Kind regards,

Eric

ramecare 05-07-2011 06:29 AM

Yes other commands works in the shell script except echo "test">>/var/log/kannan, any idea about it,can any one help me on this.

Thanks,

EricTRA 05-07-2011 06:35 AM

Hi,

Try using echo from within the script but without redirection, that is only:
Code:

echo "checking echo command"
I thought that it might be a path problem and was going to advice to use full paths but normally the echo command and the mount command are in the same directory (/bin) so that should be OK. Check just to make sure with:
Code:

which echo
which mount

in a terminal to verify that they are in the same path. If the mount command is working, then so should the echo command.

Kind regards,

Eric

ramecare 05-07-2011 06:44 AM

This was the command iam running ./newshell ramkannan Linux123@ //10.200.1.125 /MT /test root ecare2@ employeedb
#/bin/bash
mount -t cifs -o user="$1",password="$2" "$3"/"$1" "$4"
echo "test"
cd "$4""$5"
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
mysqldump -u"$6" -p"$7" "$8" > $date.sql
tar -zcvf $date.sql.tgz $date.sql
rm -rf $date.sql
As u told i used this echo "test" command in the program, but i could not get the echo output but other all the the output iam getting,any idea wat would be the problem.

[root@itsupport ~]# which echo
/bin/echo
[root@itsupport ~]# which mount
/bin/mount
[root@itsupport ~]#


Thanks

EricTRA 05-07-2011 06:57 AM

Hi,

Just looking at your posts again I noticed this:
Code:

#/bin/bash
Is that a typo? It should be
Code:

#!/bin/bash
Change it if it's a typo and try again.

Kind regards,

Eric

ramecare 05-07-2011 07:51 AM

It's working now,Thank u very much

EricTRA 05-07-2011 07:54 AM

Hi,

Great to hear you've got it fixed. Sorry I didn't notice before what was missing. Have fun with Linux.

Kind regards,

Eric


All times are GMT -5. The time now is 09:45 PM.