如何通过shell脚本启动Symfony命令

huangapple go评论47阅读模式
英文:

How to lunch symfony command via shell script

问题

我有一个Symfony命令,我想要从一个shell脚本中启动它。

示例:symfony mycommand。

我在项目文件夹中有一个shell脚本,并将Symfony命令作为参数传递给它。

这个sh脚本:

time=$1
php_script_path=$2
log_file="script.log"

# 无限循环
while true; do
    # 循环运行PHP脚本,直到达到超时时间
    while timeout $time php $php_script_path; do
        # 仅当PHP脚本在超时时间内成功退出时执行此代码
        echo "$(date): PHP脚本成功执行" >> $log_file
    done

    # 仅当PHP脚本在超时时间内未成功退出时执行此代码
    if [ $? -eq 124 ]; then
        echo "$(date): PHP脚本超时" >> $log_file
    else
        echo "$(date): PHP脚本完成时出现错误" >> $log_file
    fi
done
英文:

i have a symfony command that i want to lunch it from a shell script.

exemple: symfony mycommand.

i have a script shell figuring in the project folder and i'm passing to it the symfony command to execute as an argument.

the sh script:

time=$1
php_script_path=$2
log_file="script.log"

# Infinite loop
while true; do
    # Loop over the PHP script until the timeout is reached
    while timeout $time php $php_script_path; do
        # This code will only execute if the PHP script exits successfully within the timeout period
        echo "$(date): PHP script executed successfully" >> $log_file
    done

    # This code will only execute if the PHP script did not exit successfully within the timeout period
    if [ $? -eq 124 ]; then
        echo "$(date): PHP script timed out" >> $log_file
    else
        echo "$(date): PHP script completed with an error" >> $log_file
    fi
done

答案1

得分: 1

你可以使用与cron命令相同的方式。请在以下PHP脚本中设置您的项目路径。

time=$1
php_script_path="/var/www/project/bin/console app:your_command_name"
log_file="script.log"

# 无限循环
while true; do
    # 在超时时间内循环执行PHP脚本
    while timeout $time php $php_script_path; do
        # 仅当PHP脚本在超时时间内成功退出时执行此代码
        echo "$(date): PHP脚本成功执行" >> $log_file
    done

    # 仅当PHP脚本在超时时间内未能成功退出时执行此代码
    if [ $? -eq 124 ]; then
        echo "$(date): PHP脚本超时" >> $log_file
    else
        echo "$(date): PHP脚本完成并出现错误" >> $log_file
    fi
done
英文:

You can use the same as the cron command. Please set your project path in below PHP script.

time=$1
php_script_path="/var/www/project/bin/console app:your_command_name"
log_file="script.log"

# Infinite loop
while true; do
    # Loop over the PHP script until the timeout is reached
    while timeout $time php $php_script_path; do
        # This code will only execute if the PHP script exits successfully within the timeout period
        echo "$(date): PHP script executed successfully" >> $log_file
    done

    # This code will only execute if the PHP script did not exit successfully within the timeout period
    if [ $? -eq 124 ]; then
        echo "$(date): PHP script timed out" >> $log_file
    else
        echo "$(date): PHP script completed with an error" >> $log_file
    fi
done

huangapple
  • 本文由 发表于 2023年3月3日 18:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定