在centos7中运行php脚本作为systemd服务

时间:2015-07-20 15:18:49

标签: php centos7 systemd

我正试图在centos7启动时运行一个phpscript。目前systemd进程如下所示

[Unit]
Description=custom Service
After=network.target

[Service]
Type=forking
User=root
ExecStart=/usr/bin/php /var/www/htdocs/mysite/public/index.php abc xyz >> /var/log/custom.log 2>&1 


[Install]
WantedBy=multi-user.target

但是上面的脚本没有传递参数。我该如何解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:4)

作为替代方案,我创建了一个myphp.sh bash脚本

#!/bin/bash
nohup /usr/bin/php /var/www/htdocs/mysite/public/index.php abc xyz & >> /var/log/custom.log 2>&1

然后在systemd脚本中

[Unit]
Description=custom Service
After=network.target

[Service]
Type=forking
User=root
ExecStart=/etc/init.d/myphp.sh

[Install]
WantedBy=multi-user.target

答案 1 :(得分:1)

尝试使用此配置

[Service]
Type=forking
User=root
Environment="abc xyz"
ExecStart=/usr/bin/php /var/www/htdocs/mysite/public/index.php $PHP_PARAM_1 $PHP_PARAM_2>> /var/log/custom.log 2>&1 

<强>更新

<!DOCTYPE html>
<html>
    <head>
        <title>Ticket Log In</title>
        <script src = "../../jquery.js"></script>
<style>
body{
    padding:0;
    margin:0;
}
#box {
    background:blue;
    text-align:center;
    padding:10px;
    color:white;
    width:500px;
    height:300px;
    margin: 0 auto;

}
#LogIn {
    background:blue;
    text-align:center;
    padding:10px;
    color:white;
    width:500px;
    height:300px;
    margin: 0 auto;
}
</style>
<script>
    $(document).ready(function(){
        $("#sub").click(function(){
            var fist_name = $("#firstname").val();
            var last_name = $("#lastname").val();
            var user_email = $("#email").val();
            var department_id = $("#department").val();
            var user_pass = $("#pass").val();
            var user_pass2 = $("#pass2").val();
            $.post("LogInHelper.php",{firstname:first_name,lastname:last_name,email:user_email,department:department_id,pass:user_pass,pass2:user_pass2},function(data){
                $("#result").html(data);
            });
        });
        $("#sub2").click(function(){
            var loginuser_email = $("#loginemail").val();
            var loginuser_pass = $("#loginpass").val();
            $.post("LogInHelper.php",{loginemail:loginuser_email,loginpass:loginuser_pass},function(data){
                $("#result2").html(data);
            });
        });
        $('#box').hide();
        $('.new').click(function() {
            $('#box').toggle();
            $('#LogIn').toggle();
});
    });
</script>
    </head>
<body>
    <div id="box">
        <input type ="submit" class = "new" value = "New User Register">
        <h2>New User Register Here:</h2>
        <input type ="text" name="firstname" id="firstname" placeholder="Enter Your First Name"/></br>
        <input type ="text" name="lastname" id="lastname" placeholder="Enter Your Last Name"/></br>
        <input type ="text" name="email" id="email" placeholder="Enter Your email"/></br>
        <form action="LogInHelper.php" method="post">
        <select id = "department" name = "department">
        <?php 
                    $servername = "localhost";
                    $username = "quantco_Ted";
                    $password = "Quantum1";
                    $database = "quantco_Interns";

                    $con = mysqli_connect($servername,$username,$password,$database);
                        if($con->connect_error){
                        die("Connection failed " . $con->connect_error);
                    } 
                    $sql = "select Department_name,id from Department";
                    $result = mysqli_query($con,$sql);
                    while ($row = mysqli_fetch_array($result)) {
                        $department = $row['Department_name'];
                        $id = $row['id'];
                        echo "<option value = '$id'>$department</option>";
                    }
        ?></select></form>
        <input type ="password" name="pass" id="pass" placeholder="Enter Your Password"/>
        <input type ="password" name="pass2" id="pass2" placeholder="Re-Enter Your Password"/>
        </br></br>
        <input type ="submit" name = "sub" value = "Register" id = "sub"/>
        <div id="result"></div>
    </div>
    <div id="LogIn">
        <input type ="submit" class = "new" value = "New User Register">
        <h2>Registered User Log In:</h2>
        <input type ="text" name="loginemail" id="loginemail" placeholder="Enter Your Email"/></br>
        <input type ="password" name="loginpass" id="loginpass" placeholder="Enter Your Password"/>
        </br></br>


        <input type ="submit" name = "sub2" value = "Submit" id = "sub2"/>
        <div id="result2"></div>
    </div>
</body>
</body>
</html>