300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > mysql备份脚本 shell_linux中mysql备份shell脚本代码 相关自动化脚本

mysql备份脚本 shell_linux中mysql备份shell脚本代码 相关自动化脚本

时间:2024-04-20 09:26:14

相关推荐

mysql备份脚本 shell_linux中mysql备份shell脚本代码  相关自动化脚本

linux下监视进程 崩溃挂掉后自动重启的shell脚本

如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题。在Linux系统中,强大的shell就可以很灵活的处理这样的事务。

下面的shell通过一个while-do循环,用ps -ef|grep 检查loader进程是否正在运行,如果没有运行,则启动,这样就保证了崩溃挂掉的进程重新被及时启动。

必须注意两点:

1、ps |grep 一个进程时必须加上其路劲,否则容易grep到错误的结果;

2、必须用 -v 从结果中去除grep命令自身,否则结果非空。

#!/bin/sh#=====================#YuanHui.HE

#khler@

#=====================

while:do

echo "Current DIR is"$PWD

stillRunning=$(ps -ef |grep "$PWD/loader" |grep -v "grep")if [ "$stillRunning" ] ; then

echo "TWS service was already started by another way"

echo "Kill it and then startup by this shell, other wise this shell will loop out this message annoyingly"

kill -9 $pidof $PWD/loaderelse

echo "TWS service was not started"

echo "Starting service ..."$PWD/loaderecho "TWS service was exited!"

fi

sleep 10

done

如果启动此shell时发现进程已经存在,说明以别的方式启动了进程而不是此shell,那么它会持续提醒找到进程,解决办法是,要么只用此shell启动服务,要么一经发现以其他方式启动的服务即kill掉,上面的语句就是这么干的:

kill -9 $pidof $PWD/loader

判断文件是否存在的shell脚本代码

实现代码一、

#!/bin/sh# 判断文件是否存在

# link:

#date:/2/28myPath="/var/log/httpd/"myFile="/var /log/httpd/access.log"# 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限if [ ! -x "$myPath"]; then

mkdir "$myPath"

fi# 这里的-d 参数判断$myPath是否存在if [ ! -d "$myPath"]; then

mkdir "$myPath"

fi# 这里的-f参数判断$myFile是否存在if [ ! -f "$myFile" ]; then

touch "$myFile"

fi# 其他参数还有-n,-n是判断一个变量是否是否有值if [ ! -n "$myVar" ]; then

echo "$myVar is empty"exit0

fi# 两个变量判断是否相等if [ "$var1" = "$var2" ]; then

echo '$var1 eq $var2'

else

echo '$var1 not eq $var2'

fi

实现代码二、

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹if [ ! -d "/myfolder" ]; then

mkdir /myfolderfi#shell判断文件,目录是否存在或者具有权限

folder="/var/www/"

file="/var/www/log"#-x 参数判断 $folder 是否存在并且是否具有可执行权限if [ ! -x "$folder"]; then

mkdir "$folder"

fi#-d 参数判断 $folder 是否存在if [ ! -d "$folder"]; then

mkdir "$folder"

fi#-f 参数判断 $file是否存在if [ ! -f "$file" ]; then

touch "$file"

fi#-n 判断一个变量是否有值if [ ! -n "$var" ]; then

echo "$var is empty"exit0

fi# 判断两个变量是否相等if [ "$var1" = "$var2" ]; then

echo '$var1 eq $var2'

else

echo '$var1 not eq $var2'

fi

-f 和-e的区别

Conditional Logic on Files

-a file exists.

-b file exists and is a block special file.

-c file exists and is a character special file.

-d file exists and is a directory.

-e file exists (just the same as -a).

-f file exists and is a regular file.

-g file exists and has its setgid(2) bit set.

-G file exists and has the same group ID as this process.

-k file exists and has its sticky bit set.

-L file exists and is a symbolic link.

-n string length is not zero.

-o Named option is set on.

-O file exists and is owned by the user ID of this process.

-p file exists and is a first in, first out (FIFO) special file or

named pipe.

-r file exists and is readable by the current process.

-s file exists and has a size greater than zero.

-S file exists and is a socket.

-t file descriptor number fildes is open and associated with a

terminal device.

-u file exists and has its setuid(2) bit set.

-w file exists and is writable by the current process.

-x file exists and is executable by the current process.

-z string length is zero.

是用 -s 还是用 -f 这个区别是很大的!

在指定目录查找指定后缀文件的shell脚本代码

#!bin/sh# 在指定位置查找指定后缀的文件,包括子目录

# 用法:

# findf $1 $2# 第一个参数为后缀

# 查找指定后缀的文件并打印出来

# link:

#date:/2/26f()

{

list=`find $2|grep "/.$1/>"`for i in$listdo

echo$idone}

# 打印用法

print()

{echo "用法:"

echo "$1 /$1 /$2"

echo "第一个参数为指定的后缀名,如'h'"

echo "第二个参数为指定的目录,如果省略此参数则默认为当前目录"exit-1}

# 在当前目录查找

f1()

{

f"$1" "*"}

# 在指定的目录查找

f2()

{

cd $2f"$1" "*"}if [ "$#" -lt "1"]then

echo "给定的参数太少,最少需要一个参数."print"$0"

fi

if [ "$#" -gt "2"]then

echo "给定的参数太多,最多需要二个参数."print"$0"

fi

if [ "$#" -eq "1"]thenf1 $1exit0

fi

if [ "$#" -eq 2]thenf2 $1 $2exit0

fi

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。