标签存档: shell - 第2页

PHP中变量值为“假”的情况

$a = 0;
$b = "0";
$c = "";
$d = false;
$e = null;
$f = [];
$g = array();

echo $a?"0 is true":"0 is false \n";
echo $b?"'0' is true":"'0' is false \n";
echo $c?"'' is true":"'' is false \n";
echo $d?"false is true":"false is false \n";
echo $e?"null is true":"null is false \n";
echo $f?"[] is true":"[] is false \n";
echo $g?"array() is true":"array() is false \n";
echo $f===$g?"[] === array() \n":"[] !== array() \n";

运行结果:

0 is false
'0' is false
'' is false
false is false
null is false
[] is false
array() is false
[] === array()

监测服务器的shell脚本

#! /bin/bash
#jiance.sh

#硬盘使用率
ds=`df|awk '{if(NR==4){print int($5)}}'`
#内存使用率
used=`free -m|awk '{if(NR==2){print int($3)}}'`

tot=`free -m|awk '{if(NR==2){print int($2)}}'`

per=$(($used*100/$tot))
#apache2是否正常运行
apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
#mysql是否正常运行
mysql=`netstat -tanpl | grep 3306 | awk '{print $4}' | sed -n '1p'`
#统计
res1="硬盘使用率:"$ds"%\n内存使用率:"$per"%\n"

#echo -e $res1

if [ $apa != "0.0.0.0:80" ];then
        str1="apache2已经停止运行"
else
        str1="apache2正常运行"
fi

if [ $mysql != "127.0.0.1:3306" ];then
        str2="myqsl已经停止运行"
else
        str2="mysql正常运行"
fi
#统计输出
text=$res1$str1"\n"$str2;
#监控发邮件
if [ $ds  -gt 45 ];then
        echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi

if [ $per -gt 55 ];then
        echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi

关于SQLSTATE[HY000] [1698]: Access denied for user ‘root’@’localhost’的问题

问题描述:

客户端可以登录,PHP无法登录。

解决方法:

select user, plugin from mysql.user;
update mysql.user set plugin = 'mysql_native_password' where user='root';

重启msyql。

php递归读取目录及子目录下所有文件

<?php
function searchDir($path,&$data){
        if(is_dir($path)){
                $dp=dir($path);
                //如果子目录可读,则递归读取该目录
                while($file=$dp->read()){
                        if($file!='.'&& $file!='..'){
                                searchDir($path.'/'.$file,$data);
                        }
                }

        $dp->close();
        }
        if(is_file($path)){
                $data[]=$path;
        }
}

function getDir($dir="."){
        $data=array();
        searchDir($dir,$data);
        return   $data;
}
fwrite(STDOUT,"Please input dir name:");
$dir = trim(fgets(STDIN));
print_r(getDir($dir));

        用法:将以上代码保存为dir.php,在命令模式下执行该文件。

# php dir.php
# Please input dir name: .
Array
(
    [0] => ./cms0517.zip
    [1] => ./.bash_logout
    [2] => ./cms519.zip
    [3] => ./.Xauthority
    [4] => ./.cache/motd.legal-displayed
    [5] => ./.bash_history
    [6] => ./.ssh/authorized_keys
    [7] => ./dir.php
    [8] => ./.sudo_as_admin_successful
    [9] => ./.bashrc
    [10] => ./.profile
)

        结果以一维数组的方式遍历出dir.php文件夹下和其子文件夹下的文件。

用crontab实现hotsun.link自动备份打包

        准备好要自动执行数据库备份的脚本:

#!/bin/bash

user="root";

password="******";

dbname="blog";
#定义文件名
filename=$dbname$(date +%Y%m%d_%H%M)".sql";
#要存放的文件夹
dir="/var/www/html/";
#检查dir
if !(test -d $dir)
then
mkdir $dir;
chmod 755 -R $dir;
fi
#找到两天之内的sql文件并删除
find $dir -name "*.sql"  -mtime -2 -exec rm -f {} \;
#备份
mysqldump -u$user -p$password $dbname>$dir$filename;

        准备好要自动执行网站打包的脚本

#!/bin/bash

dir="/var/www";
#删除期限超出15天的7z文件
find $dir -name "*.7z" -mtime +15 -exec rm -f {} \;
#用7z高压缩率备份,占用较小空间
7z a -t7z -r $dir"/wp.cpxiang.tech"$(date +%Y%m%d)".7z" $dir"/html/*";

        配置crontab

# crontab -e
#加入以下代码
#每天17:30执行"/home/ubuntu/auto_backup.sh"
30 17 * * * /home/ubuntu/auto_backup.sh
#每个星期五的17:32执行"/home/ubuntu/auto_7z.sh"
32 17 * * 5 /home/ubuntu/auto_7z.sh