最近一段时间我再仔细看了一下shell脚本的高级教程,发现有很多新的成果。
要调试shell程序,请使用以下命令查看已运行的所有进程: 是方便的命令。
sh -x -v example.sh
1 .使用shell将信息写入Syslog日志文件
该APP应用程序使用Syslog协议将信息发送到位于/var/log目录中的Linux系统日志文件。 Sysklogd提供了两个系统工具: 一个是系统日志,另一个是内核信息捕获。 大多数程序通常使用c语言或Syslog APP应用程序或库发送syslog消息。
如何使用shell将信息:写入Syslog日志文件
(1) .使用Logger命令
Logger命令是shell命令(接口)。 通过此接口,可以使用Syslog的系统日志模块。 也可以直接从命令行将信息写入系统日志文件。
例如,记录硬盘升级后的系统重新启动信息:
$ loggersystemrebootedforharddiskupgrade
可以查看/var/log/message文件:
# tail -f /var/log/message
输出为:
Jan 262033605:31 Dell 6400 logger :系统内存bootedforharddiskupgrade
也可以在脚本程序中使用Logger命令。 请看下面的示例:
#! /悲伤的跳糖/bash
HDBS='db1 db2 db3 db4 '
BAK='/sout/email '
[! -d $BAK ] mkdir -p $BAK || :
/悲伤的跳糖/rm $BAK/*
NOW=$(date '%d-%m-%Y ' )
ATTCH='/sout/backup.$NOW.tgz '
[ -f $ATTCH ] /难过的跳糖/rm $ATTCH || :
MTO='you@yourdomain.com '
for db in $HDBS
德奥
FILE='$BAK/$db.$NOW-$(date '%T ' ).gz '
mysqldump-uadmin-p ' password ' $ db|gzip-9 $ file
道恩
tar -jcvf $ATTCH $BAK
mutt-s ' db $ now '-a $ at tch $ MTO DBS $ (date )
欧洲足球锦标赛
[ '$?' !='0' ] logger ' $0- mysqlbackupfailed '|| :
如果MySQL数据库备份失败,上面最后一行代码会将信息写入/var/log/message文件。
(2) .其他使用方法
如果需要记录/var/log/myapp.log文件中的信息,请使用:
$ logger -f /var/log/myapp.log
向系统日志:等画面发送消息
$ logger -s 'Hard disk full '
浏览man浏览页面,获取更多选项信息:
man logger
man syslogd
[root@cnc-squid2 ~]# man logger
Logger(1) BSD通用命令手册Logger(1)1)。
名字
logger-ashellcommandinterfacetothesyslog (3)系统日志模块
SYNOPSIS
logger [-isd ] [-ffile ] [-ppri ] [-ttag ] [-u socket ] [消息. ]
描述
loggermakesentriesinthesystemlog.itprovidesashellcommandinterfacetothesyslog (3)系统日志模块。
Options:
- ilogtheprocessidoftheloggerprocesswitheachline。
- s日志the message to standard error,as well as the system log。
- f文件log the specified file。
- pprienterthemessagewiththespecifiedpriority.theprioritymaybespecifiednumericallyorasa ' ' facil -
ity.level'' pair. For example,'-p local3. info ' ' logs the message (s ) asinformationallevelinthelocal3facil -
ity. The default is ''user.notice.' '
- ttagmarkeverylineinthelogwiththespecifiedtag。
- usockwritetosocketasspecifiedwithsocketinsteadofbuiltinsyslogroutines。
- duseadatagraminsteadofastreamconnectiontothissocket。
- endtheargumentlist.thisistoallowthemessagetostartwithahyphen (-)。
消息写入the消息到日志; if not specified,and the -f flag is not provided,标准输入is logged。
theloggerutilityexits0on success,and 0 if an error occurs。
Valid facility names are: auth,authpriv (forsecurityinformationofasensitivenature ),cron,daemon,ftp,kern,lpr,
mail,news,security (deprecatedsynonymforauth ),syslog,user,uucp,and local0 to local7,inclusive。
validlevelnamesare(:alert、crit、debug、emerg、err、error(deprecatedsynonymforerr )、info、notice、Panic ) DD
synonym for emerg (,warning,warn (deprecatedsynonymforwarning ).forthepriorityorderandintedpurposesofthese
级别,seesyslog(3)。
EXAMPLES
logger系统重启