简体   繁体   English

如何在php中设置cron作业(适用于iPhone应用程序)

[英]How to set up cron job in php(for iPhone application)

In iOS application, I have to set up cron for sending notification to particular device after every 5 minute.I am using frapi API . 在iOS应用程序中,我必须设置cron以每5 分钟发送一次通知到特定设备。我正在使用frapi API I searched on it and found that i have to create cron entry like this : 我对其进行搜索,发现我必须创建cron条目,如下所示:

/etc/cron.d/

*/5 * * * * root cd /path_to_your_script/ && php your_script.php >> /var/some.log &2>&1

I wonder where and how can i set above commands ? My script is ready to send notification as below : 我的脚本可以发送通知了,如下所示:

<?php

// Device token:
$deviceToken = 'xxxxxx';

$passphrase = 'xxxxx';
$badge = 1;

// Displays alert message here:
$message = 'Match Found!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert',       '/Users/Documents/iOS_Application_Developement/new/APNSPHP/ApnsPHP-master/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'badge' => $badge,
'sound' => 'default'
);


// Encode the payload as JSON
 $payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .      $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
  echo 'Message not delivered' . PHP_EOL;
else
  echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

?> ?>

my php script is ready but I don't know how to run it after every 5 minutes.I am using Frapi API.Doed anyone know how to set up and run Cronjob in Frapi API(php and iOS). 我的php脚本已经准备好,但是每5分钟我不知道如何运行它。我正在使用Frapi API。是否有人知道如何在Frapi API(php和iOS)中设置和运行Cronjob。

I am newbie to this cron job..so any help will be appreciated. 我是这项Cron工作的新手。因此,我们将不胜感激。

You need to check whether you have crontab or not so for this you can 您需要检查是否具有crontab,因此可以

check on terminal by typing 通过键入检查终端

crontab -l

if it gives you have no cron tab then you need to download it. 如果它没有cron选项卡,则需要下载它。 for mac system you can download cronix from here 对于Mac系统,您可以从此处下载cronix

and add your command and set time interval whatever you want with your command. 并添加您的命令并根据您的命令设置时间间隔。

*Note: This answer assumes your server is running either unix or linux, if it is a Windows server please leave a comment and I will write up a Windows version. *注意:此答案假设您的服务器运行的是Unix或Linux,如果是Windows服务器,请发表评论,我将编写Windows版本。

*Note 2: This answer also assumes that the php script you want to run is located in /var/www , if this is not the case replace all instances of /var/www with the folder containing your php script. *注2:此答案还假设您要运行的php脚本位于/var/www ,如果不是这种情况,请用包含您的php脚本的文件夹替换/var/www所有实例。


As Ishu mentioned, the first step is to check and see if crontab is supported on your server; 正如Ishu所提到的,第一步是检查服务器上是否支持crontab run the following command in the terminal (via. SSH or in the console if you have physical access) to do so. 为此,请在终端中运行以下命令(通过SSH或控制台,如果您具有物理访问权限)。

crontab -l

If this command returns some message about there being no cron tasks set, great! 如果此命令返回有关未设置cron任务的消息,那就太好了! Otherwise, Google around to find out how to install crontab on whatever OS your server is running and do so. 否则,谷歌四处寻找如何在服务器运行的任何操作系统上安装crontab。

If you would like to log the output of the php script so that you can check it, create a logs folder in the same directory as the php script and set its permissions so that it is writeable. 如果要记录php脚本的输出以便可以对其进行检查,请在与php脚本相同的目录中创建一个logs文件夹,并设置其权限以便可写。

cd /var/www/
mkdir logs
chmod 660 logs

Once you have confirmed that crontab is working, you can set the cron job by running the following command in the terminal. 确认crontab正常工作后,您可以通过在终端中运行以下命令来设置cron作业。

crontab -e

This will open up a text editor which should at this point be empty. 这将打开一个文本编辑器,此时应为空。 Enter the following code in the editor: 在编辑器中输入以下代码:

*/5 * * * * cd /var/www && /usr/bin/php your_script.php &> logs/cron.log

Replace your_script.php with the name of your php script. 用您的php脚本名称替换your_script.php If you do not want logging replace logs/cron.log with /dev/null . 如果您不想记录logs/cron.log/dev/null替换logs/cron.log

If your php installation is not located at /usr/bin/php , change that to wherever the php binary on your server is located. 如果您的php安装不在/usr/bin/php ,请将其更改为服务器上php二进制文件所在的位置。

Once you have setup everything the way you like, run whatever the "save and exit" command is for the editor crontab is using. 一旦按照自己的喜好设置了所有内容,请为crontab使用的编辑器运行“保存并退出”命令。 Confirm that the job was set successfully using the following command. 使用以下命令确认作业已成功设置。

crontab -l

Your cron job is now setup and will run /var/www/your_script.php every 5 minutes. 现在,您的cron作业已设置/var/www/your_script.php ,将每5分钟运行一次/var/www/your_script.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM