######################################################################################################################
2009-05-06
######################################################################################################################
5、提交一个任务在后台运行--nohup命令
nohup命令:不挂断地运行命令。
如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,
那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。
nohup就是不挂起的意思( no hang up)。
该命令的一般形式为:nohup command &
使用nohup命令提交作业
如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,
除非另外指定了输出文件:
nohup command > myout.file 2>&1 &
在上面的例子中,输出被重定向到myout.file文件中。
2 > &1 :重定向将标准的错误信息输出到标准的输出设备中。2 代表标准的错误设备,1代表标准的数据设备
& :在后台运行任务
例如:
[root@localhost opt]# nohup ./run_sql.ksh &
[1] 30161
[root@localhost opt]# nohup: appending output to ‘nohup.out’
[1]+ Done nohup ./run_sql.ksh
上述执行成功,将执行结果输出到 nohup.out文件中
例如:
[root@localhost opt]# nohup ./run_sql.ksh >logfile.1st 2>&1 &
[1] 30285
[root@localhost opt]#
[1]+ Done nohup ./run_sql.ksh >logfile.1st 2>&1
注释:
nohup:提交一个任务以便在即使断开终端会话之后该任务能够继续运行
run_sql.ksh:定义想让运行在后台的脚本
> logfile.lst:重定向将标准数据结果输出到指定的文件
2 > &1 :重定向将标准的错误信息输出到标准的输出设备中。2 代表标准的错误设备,1代表标准的数据设备
& :在后台运行任务
6、查看后台进程的执行情况--tail -f
tail -f:可以动态地显示文件中最后新出现的行,此命令是会不停地刷新。
注:CTRL+C 表示结束显示
假设nohup ./run_sql.ksh >logfile.1st 2>&1 &命令中将输出结果输出到 logfile.1st文件中,
此时可以使用tail -f命令可以持续的来查看输出到 logfile.1st中的新出现的内容,方便查看
后台进程的进展情况:
[root@localhost opt]# tail -f logfile.1st
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> SQL>
DBID NAME CREATED
---------- --------- ---------
1268442274 STAPLES 19-SEP-08
SQL> SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
7、调试含有输入参数的SHELL脚本
[root@localhost opt]# cat check_parms.ksh
#Exit if no first parameter $1
if [ -z "$1" ]
then
echo "Usage:check_parms.ksh the first parameter is ORACLE_SID"
exit 99
fi
#Exit if no second parameter $2
if [ -z "$2" ]
then
echo "Usage:check_parms.ksh the second parameter is <#_days> (where value is >100)"
exit 99
fi
#Exit if parm is not greater than 100.
tmp=' '$2'' #Convert string to number
if [ $tmp -lt 101 ]
then
echo
echo "Argument two is less than 100./
Aborting Script."
echo
exit 99
fi
--检查语法是否有误的调试 sh -n
[oracle@localhost opt]$ sh -n ./check_parms.ksh
--检查脚本执行情况,并且显示脚本中所有变量的值
[oracle@localhost opt]$ sh -x ./check_parms.ksh STAPLES 1200
+ '[' -z STAPLES ']'
+ '[' -z 1200 ']'
+ tmp=' 1200'
+ '[' 1200 -lt 101 ']'
调试方法:
最简单的调试命令当然是使用echo命令。
您可以使用echo在任何怀疑出错的地方打印任何变量值。
这也是绝大多数的shell程序员要花费80%的时间来调试程序的原因。
Shell程序的好处在于不需要重新编译,插入一个echo命令也不需要多少时间。
shell也有一个真实的调试模式。如果在脚本"strangescript" 中有错误,您可以这样来进行调试:
sh -x strangescript
这将执行该脚本并显示所有变量的值。
shell还有一个不需要执行脚本只是检查语法的模式。可以这样使用:
sh -n your_script
这将返回所有语法错误。
下面对脚本进行修改:
将 tmp=' '$2'' #Convert string to number
改为
tmp=' expr '$2'' #Convert string to number
[oracle@localhost opt]$ sh -x ./check_parms.ksh STAPLES 1200
+ '[' -z STAPLES ']'
+ '[' -z 1200 ']'
+ tmp='expr 1200'
+ '[' expr 1200 -lt 101 ']'
./check_parms.ksh: line 17: [: too many arguments
此时会报执行错误,并显示出在执行过程中所有变量的值
下面继续测试上述脚本:
--不输入第一个参数
[oracle@localhost opt]$ ./check_parms.ksh
Usage:check_parms.ksh the first parameter is ORACLE_SID
--不输入第二个参数
[oracle@localhost opt]$ ./check_parms.ksh STAPLES
Usage:check_parms.ksh the second parameter is <#_days> (where value is >100)
--输入的第二个参数值小于100
[oracle@localhost opt]$ ./check_parms.ksh STAPLES 90
Argument two is less than 100. Aborting Script.
上述将脚本中的所有路径都测试到了!
8、确保只有oracle用户才能运行的判断脚本
在上述的check_parms.ksh文件中最前添加下面的SHELL代码:
if [ `whoami` != 'oracle' ]
then
echo "Error:You must be oracle to execute."
exit 99
fi
注意:
1)上面的`符号要与'符号相区别:
`是TAB键上的符号,'是单引号,要注意单引号和TAB键上的符号相区别
注意单引号和`(tab键上面的那个)的区别
2)!=前后都要有空格
调试过程如下:
oracle用户下:
[oracle@localhost opt]$ sh -x ./check_parms.ksh STAPLES 1200
++ whoami
+ '[' oracle '!=' oracle ']'
+ '[' -z STAPLES ']'
+ '[' -z 1200 ']'
+ tmp=' 1200'
+ '[' 1200 -lt 101 ']'
root用户下:
[root@localhost opt]# sh -x ./check_parms.ksh STAPLES 1200
++ whoami
+ '[' root '!=' oracle ']'
+ echo 'Error:You must be oracle to execute.'
Error:You must be oracle to execute.
+ exit 99
unix_for_oracle_dbas读书笔记-2
最新推荐文章于 2025-08-07 20:56:18 发布