Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

small Script problem

Status
Not open for further replies.

Themuppeteer

Programmer
Joined
Apr 4, 2001
Messages
449
Location
BE
Hello,

I have a program that executes commands. If a command does not work, I want it to be logged. So I do something like this: ./myprog 2>> /var/log/messages
Which works fine, except for the fact that I want the log message to be more complete then 'could not connect' (the error that the program produces). I would like it to be something like: "myprogram: 14.22 could not connect"
How do I do this ? I know I should cat the error with the time and name but I don't know how.
Can NE1 help me?

thnx.

Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
You will need to capture stderr and the check the error level, so replace...
[tt]
./myprog 2>> /var/log/messages
[/tt]
with....
[tt]
./myprog 2>> /tmp/message
[ $? -ne 0 ] && echo myprogram: 14.22 $(</tmp/message >> /var/log/messages
[/tt]


 
Sorry,I missed a closing bracket...
[tt]
./myprog 2>> /tmp/message
[ $? -ne 0 ] && echo myprogram: 14.22 $(</tmp/message) >> /var/log/messages
[/tt]
 
The correct way to log messages in /var/log/messages is using the logger command, which sends the error message to the syslogd which in turn logs it in the appropriate places, as defined in /etc/syslog.conf.

Usually the &quot;daemon.notice&quot; priority is set to log to /var/log/messages, so you would type:

[tt]logger -p daemon.notice -t myproc.c &quot;An error message.&quot;[/tt]

-t specifies a &quot;tag&quot; or programme name to associate with the error message. You may also want to include the -i switch to include it's process ID.
Annihilannic.
 
Hello Annihilannic
So I could just type on the prompt:

logger -p daemon.notice ./myprogram

and it should work ?
Just tried that and doesnt work, I just end op on the command prompt again.

How should the command be then ?

thnx Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
Ok, I didnt manage to get Ygor 's script to work. It does not give errors,but it doesn't do a thing.

The logger thing might be the ay to go but I did not get a reply today (and I have been working on it all day).

So eventually I found a way to get it o work.

As I said, my program executes commands. Those commands have sometimes errors. I execute the command with 'popen' and the command has a 2>&1 at the end. Then I read the output from it and write it to the log using syslog(TYPE_ERR,theError);

Thnx a lot for your time and effort guys!
Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
Yes, you end up at the command prompt again, but did you check whether anything had appeared in /var/log/messages?

If you want the message to be displayed to the user as well you will also need to echo it. Annihilannic.
 
yes I checked, and no nothing was added...
My program is a server that should never stop running, so being back at the command prompt isn't a good thing... or do I miss something ? Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
What operating system?

Is the &quot;daemon.notice&quot; priority configured in /etc/syslog.conf. If not, choose another appropriate one, or create your own &quot;local0.notice&quot; or similar.

(I've never understood why syslog isn't more flexible and doesn't allow you to define priorities with your own names instead of local0, local1...)

If you do have to edit it, note that syslogd only understands tabs, not spaces. Annihilannic.
 
Its RED HAT 7.2
No, its not configured in the /etc/syslog.conf.
But we don't have to worry about it anymore since the 'syslog' command in my C++ program works just fine.
I execute the command using 'popen' and then I write
the feedback to the log, so problem solved.

fyi I added the file.

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure

# Log all the mail messages in one place.
mail.* /var/log/maillog


# Log cron stuff
cron.* /var/log/cron

# Everybody gets emergency messages
*.emerg *

# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler

# Save boot messages also to boot.log
local7.* /var/log/boot.log


Thanks a lot for the reply, and your time and effort. Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top