LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
LinkBack Search this Thread
Old 05-07-2011, 04:13 AM   #1
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Rep: Reputation: 0
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,

Last edited by ramecare; 05-07-2011 at 04:15 AM.
 
Old 05-07-2011, 04:28 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,345

Rep: Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945Reputation: 1945
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.
 
Old 05-07-2011, 04:28 AM   #3
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
1 members found this post helpful.
Old 05-07-2011, 04:46 AM   #4
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Original Poster
Rep: Reputation: 0
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,
 
Old 05-07-2011, 05:13 AM   #5
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 05-07-2011, 06:13 AM   #6
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Original Poster
Rep: Reputation: 0
#/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,
 
Old 05-07-2011, 06:15 AM   #7
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hi,

Do the other commands in your script execute as expected?

Kind regards,

Eric
 
Old 05-07-2011, 06:29 AM   #8
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Original Poster
Rep: Reputation: 0
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,
 
Old 05-07-2011, 06:35 AM   #9
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 05-07-2011, 06:44 AM   #10
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Original Poster
Rep: Reputation: 0
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

Last edited by ramecare; 05-07-2011 at 06:46 AM.
 
Old 05-07-2011, 06:57 AM   #11
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 05-07-2011, 07:51 AM   #12
ramecare
Member
 
Registered: Feb 2011
Posts: 176

Original Poster
Rep: Reputation: 0
It's working now,Thank u very much
 
Old 05-07-2011, 07:54 AM   #13
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create log file from script fotoguy Programming 12 03-27-2012 06:39 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
Create log file against a script shipon_97 Linux - Newbie 1 01-02-2008 11:54 AM
Create a shell Script that log in as root mashed Linux - Newbie 1 04-08-2006 04:13 AM
Script to create a log file satimis Programming 15 01-16-2006 06:39 AM


All times are GMT -5. The time now is 05:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration