简体   繁体   中英

Crontab for php script that will run a nodejs script

I set a crontab that would run a PHP script every min to check if my steam bot is in game, and if he's not, run a node js script.

This is my crontab:

* * * * * /usr/bin/php /home/cron/checkStatus.php >/dev/null 2>&1

This is my PHP script:

shell_exec("screen -AdmS bot /usr/local/bin/node /home/node_modules/mine/script.js");

When I run the command myself, the node js script launches, however, when the crontab tries to launch the node js script, well, nothing happens. Ideas?

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

To include the HTTP module, use the require() method:

var http = require('http');

The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.

Use the createServer() method to create an HTTP server:

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

After that Use NPM To Install A Package Called PM2.

NPM is a package manager that you will use to install frameworks and libraries to use with your Node.js applications. NPM was installed with Node.js. PM2 is a sweet little tool that is going to solve two problems for you:

  1. It is going to keep your site up by restarting the application if it crashes. These crashes should NOT happen, but it is good know that PM2 has your back. (Some people may be aware of Forever.js, another tool that is used to keep node based sites running - I think you will find that PM2 has a lot to offer.)

    1. It is going to help you by restarting your node application as a service every time you restart the server. Some of use know of other ways to do this, but pm2 makes it easier, and it has some added flexibility.

Install PM2 by typing thr following at the command line:

sudo npm install pm2 -g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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