shell基础语法以及监控进程不存在重启
2016-02-28 22:20
567 查看
转码
# dos2unix ./test.sh
权限
# chmod a+x ./test.sh
语法
变量
var="111"
echo $var
echo ${var}
运算
no1=4;
no2=5;
let result=no1+no2
echo $result;
自增自减少
let no++
let no--
[]和let类似
result=$[ no1 + no2 ]
result=$[ $no1 + 5 ]
也可以使用(()),但使用(())时,变量名前需要加上$
result=$(( $no1 + 5 ))
expr也可以用于基本算术操作
result=`expr 3+4`
result=$(expr $no1 + 5)
以上不支持浮点
bc可以浮点
echo "4*0.56" |bc 输出2.24
文件描述符
0 1 2 标准输入 输出 标准错误
重定向到文本
echo "123" > temp.txt
追加
echo "123" >> temp.txt
查看文件内容
cat temp.txt
cat 1.txt 2.txt 多个内容
ls +
非法参数输出错误信息stderr
命令错误并退回 返回非0的退出状态
成功完成返回数字0
退出状态可以从特殊变量$?获得,在命令执行完后立刻运行echo $?就可以打印退出状态
ls + >out.txt 错误信息不能输出到文本
ls + 2>out.txt 错误信息能输出到文本
stderr和stdout各自
cmd 2>stderr.txt 1>stdout.txt
stderr转换为stdout
cmd 2>&1 out.txt
或者
cmd &> out.txt
stderr重定向到
/dev/null
读取日期
date
函数定义
function fname()
{
return 0;返回值
}
函数调用
$fname;
$fname arg1 arg2;传参数
一定要注意[]空格
注意<之类需要(())
下面这个监控redis进程不存在就重启
crontab -e每分钟监控一次
参考:《Linux Shell脚本攻略(第2版)》
Shell中字符串、数值的比较
linux 进程监控和自动重启的简单实现
# dos2unix ./test.sh
权限
# chmod a+x ./test.sh
语法
变量
var="111"
echo $var
echo ${var}
运算
no1=4;
no2=5;
let result=no1+no2
echo $result;
自增自减少
let no++
let no--
[]和let类似
result=$[ no1 + no2 ]
result=$[ $no1 + 5 ]
也可以使用(()),但使用(())时,变量名前需要加上$
result=$(( $no1 + 5 ))
expr也可以用于基本算术操作
result=`expr 3+4`
result=$(expr $no1 + 5)
以上不支持浮点
bc可以浮点
echo "4*0.56" |bc 输出2.24
文件描述符
0 1 2 标准输入 输出 标准错误
重定向到文本
echo "123" > temp.txt
追加
echo "123" >> temp.txt
查看文件内容
cat temp.txt
cat 1.txt 2.txt 多个内容
ls +
非法参数输出错误信息stderr
命令错误并退回 返回非0的退出状态
成功完成返回数字0
退出状态可以从特殊变量$?获得,在命令执行完后立刻运行echo $?就可以打印退出状态
ls + >out.txt 错误信息不能输出到文本
ls + 2>out.txt 错误信息能输出到文本
stderr和stdout各自
cmd 2>stderr.txt 1>stdout.txt
stderr转换为stdout
cmd 2>&1 out.txt
或者
cmd &> out.txt
stderr重定向到
/dev/null
读取日期
date
函数定义
function fname()
{
return 0;返回值
}
函数调用
$fname;
$fname arg1 arg2;传参数
一定要注意[]空格
#! /bin/sh a=4; b=5; if [ "$a" = "$b" ]; then echo "1111" #####
#! /bin/sh a=4; b=5; if [ "$a" -eq "$b" ] then echo "yyyyyyyyyy" else echo "nnnnn" fi #####
#! /bin/sh ps aux|grep redis-stat | grep -v grep| wc -l a=11 b=11 if [ $a -eq $b ] then echo "start process....." else echo "runing....." fi #####
注意<之类需要(())
下面这个监控redis进程不存在就重启
#! /bin/sh p="redis-server" proc_num() { num=`ps aux|grep $p | grep -v grep| wc -l` return $num } proc_num n=$? if (( "$n" < 1 )) then echo $n echo "start redis-server process....." else echo $n echo "redis-server runing ok." fi #####
crontab -e每分钟监控一次
*/1 * * * * /redis.sh
参考:《Linux Shell脚本攻略(第2版)》
Shell中字符串、数值的比较
linux 进程监控和自动重启的简单实现
相关文章推荐
- shell语法以及监控进程不存在重启
- “笨办法学python”学习笔记-在终端powershell中对目录进行简单的编辑(一)
- 收集的48个Shell脚本小技巧
- Bash中的shopt选项
- mac终端下运行shell脚本
- shell 练习002 (20160228)
- shell编程之文件包含
- PowerShell-Exchange:统计每天邮件收发
- 虚拟机Ubuntu和Xshell 5连接
- shell命令执行顺序
- flock导致进程卡死, 如何查看
- shell 编程之函数
- 关于在openstack的环境变量.bashrc自定自己简化命令
- WyBox使用shell脚本检测io口是否正常
- PowerShell-Exchange:Check High Item
- python结合shell查询google关键词排名的实现代码
- shell编程练习002(20160227)
- XSHELL 绝佳配色方案, 保护你的眼睛
- shell脚本实例-matrix
- bash 操作 sqlite3