Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本效率/逻辑_Bash_Shell_Openssl_Apache Config - Fatal编程技术网

Bash脚本效率/逻辑

Bash脚本效率/逻辑,bash,shell,openssl,apache-config,Bash,Shell,Openssl,Apache Config,我有一个非常具体的剧本,我大约两年前写的。它运行非常出色,从未失败过。。。然而,我们在系统中加入的网站越多,脚本速度就越慢(正如预期的那样)。。。每10分钟运行一次脚本的cron现在大约需要3分钟才能完成。我希望缩短这段时间。让我来解释一下脚本的作用 1) 以下是主循环——它检查数据库中所有付费客户的网站: mysql --login-path=main-data -e "SELECT user_file FROM database" | while read user_file; do 2)

我有一个非常具体的剧本,我大约两年前写的。它运行非常出色,从未失败过。。。然而,我们在系统中加入的网站越多,脚本速度就越慢(正如预期的那样)。。。每10分钟运行一次脚本的
cron
现在大约需要3分钟才能完成。我希望缩短这段时间。让我来解释一下脚本的作用

1) 以下是主循环——它检查数据库中所有付费客户的网站:

mysql --login-path=main-data -e "SELECT user_file FROM database" | while read user_file; do
2) 在主循环中我构建各个conf文件:

p=$user_file
echo "<VirtualHost *:80>" > /etc/apache2/sites-available/$p.conf
.........
4) 仍然在main循环中——我检查SSL并创建conf文件的这一部分。请注意,这里有一些
openssl
config测试可能会减慢速度:

mysql --login-path=main-data --skip-column-names -e"SELECT ssl FROM database
    WHERE user_file = '$user_file' 
    AND a.primary_domain = '1' 
    AND b.https = '1'" | while read ssl; do


    if [ $ssl = 1 ]
    then
        ########################### START SSL TEST
        crt="/var/www/liveSites/websites/$user_file/ssl/$domain.crt"
        key="/var/www/liveSites/websites/$user_file/ssl/$domain.key"

       key_test=$(openssl x509 -in $crt -pubkey -noout -outform pem | sha256sum 2>&1)
       crt_test=$(openssl pkey -in $key -pubout -outform pem | sha256sum 2>&1)

       if [ "$key_test" = "$crt_test" ]
       then
           echo "\n - Matched -- Cert Good - \n";
       else
           echo "SSL match failed for $user_file -> $domain" > /etc/apache2/websitesCron/ssl_fail.txt
           cat /etc/apache2/websitesCron/ssl_fail.txt | mail -s "SSL INSTALLATION ERROR" it@mycompany.com
        fi
        ####################### END SSL TEST

       echo "<VirtualHost *:443>
             ServerName    $domain
             ServerAlias   www.$domain
             DocumentRoot /var/www/liveSites/websites/$user_file/public_html
             ........
    fi
done
在主循环之后——我检查Apache配置,以确保它将
优雅地重新加载
,并检查
ssl
是否未通过
openssl
比较检查:

/usr/sbin/apachectl configtest > /etc/apache2/websitesCron/configtest 2>&1

if grep "failed" /etc/apache2/websitesCron/configtest
then
    # Do failed stuff stop script and turn cron off
elif grep "failed" /etc/apache2/websitesCron/ssl_fail.txt
then
    # Do failed SSL stuff stop script and turn cron off
else
    # gracefully reload Apache
    /etc/init.d/apache2 reload
fi

这是一个相当简单的脚本,但有几个移动的部分——对于1000多个网站来说,3分钟运行是否合理?我应该用叉子吗?是否存在可以“清理”的逻辑更改?

如果它对您有效,我不会费心更改逻辑。但您可以进行以下更改以并行运行:

  • 将整个主循环移动到一个Bash函数中,该函数接受一个参数
    user\u文件
  • 更改主循环以在后台调用此函数
你的外环看起来像这样:

mysql --login-path=main-data -e "SELECT user_file FROM database" | while read user_file; do
    mainFunction "$user_file" &
done

注意:如果1000个后台进程吞没了你的机器,那么考虑应用其中的一些。

避免你以前做过的长测试。你可以寻找不同的方法,比如

sha_dir=/var/shadir
crt="/var/www/liveSites/websites/$user_file/ssl/$domain.crt"
key="/var/www/liveSites/websites/$user_file/ssl/$domain.key"
sha_crt="${sha_dir}/${user_file}_${domain}.crt"
sha_key="${sha_dir}/${user_file}_${domain}.key"
if [[ "${crt}" -nt "${sha_crt}" ]]; then
   crt_test=$(openssl pkey -in $key -pubout -outform pem | sha256sum 2>&1 | tee "${sha_crt}")
else
   crt_test=$(cat ${sha_crt})
fi
if [[ "${key}" -nt "${sha_key}" ]]; then
   key_test=$(openssl x509 -in $crt -pubkey -noout -outform pem | sha256sum 2>&1 | tee "${sha_key}")
else
   key_test=$(cat ${sha_key})
fi

这绝对值得一看,谢谢。。这是一个相当坚固的服务器,所以我不认为每10分钟有1000多个并发作业会损害它。。我将对此进行试验..是否每次都需要重新生成所有配置文件?在我看来,你应该让它检查更改,并且只为实际更改的站点重新生成文件。试着找出方法,不要在循环中反复运行相同的命令。您可以运行一个较大的SQL脚本,并让它将所有这些输出都写入一个文件中吗?也许邮件也有类似的功能?与其忙于处理1000个作业并等待它们,不如使用GNU Parallel制作一份文件副本,在许多地方添加
echo“$SECONDS some comment”
,并制作一个时间表,这部分需要花费大量时间。
mysql --login-path=main-data -e "SELECT user_file FROM database" | while read user_file; do
    mainFunction "$user_file" &
done
sha_dir=/var/shadir
crt="/var/www/liveSites/websites/$user_file/ssl/$domain.crt"
key="/var/www/liveSites/websites/$user_file/ssl/$domain.key"
sha_crt="${sha_dir}/${user_file}_${domain}.crt"
sha_key="${sha_dir}/${user_file}_${domain}.key"
if [[ "${crt}" -nt "${sha_crt}" ]]; then
   crt_test=$(openssl pkey -in $key -pubout -outform pem | sha256sum 2>&1 | tee "${sha_crt}")
else
   crt_test=$(cat ${sha_crt})
fi
if [[ "${key}" -nt "${sha_key}" ]]; then
   key_test=$(openssl x509 -in $crt -pubkey -noout -outform pem | sha256sum 2>&1 | tee "${sha_key}")
else
   key_test=$(cat ${sha_key})
fi