1

I'm working on a simple script to choose best server based. The script works perfeclty on my computer but crontab is not working as expected.

Here is my crontab rule: * * * * * curl -s 'https://my-domain.com/server_status.php'

Here is my: server_status.php

<?php
if (isset($_POST['server_cdn'])) {
    if (!empty($_POST['server_cdn'])) {
        $path = '../player/server.txt'; 
        $f = fopen($path, "w+");
        fwrite($f, $_POST['server_cdn']); 
        fclose($f); chmod($path, 0777);
        exit();
    }
}
?>

<script>
$.getJSON("/player/servers.json", function(json) {
    var script_name = json.name;
    var script_version = json.version;
    var prefix = json.prefix;
    var _svrs = Object.keys(json.servers).length;
    var mach_available = {};

        for(i = 1; i <= _svrs; i++) {
            if (json.servers['server'+i].status == 1) {

                $.ajax({
                    url: "https://cdn" + json.servers['server'+i].cdn + "." + json.prefix + "/" + json.file,
                    type: 'POST',
                    async: false,
                    cache: false,
                    timeout: 7000,
                    error: function(){ mach_available['cdn' + json.servers['server'+i].cdn] = null; },
                    success: function(response){ 
                        mach_available['cdn' + json.servers['server'+i].cdn] = parseInt(response);
                    }
                });

            } else {
                mach_available['cdn' + json.servers['server'+i].cdn] = null;
            }
        }
        var min = Object.entries(mach_available).sort(function(x,y) {return x[1]-y[1]})[0];

        $.ajax({
            type: 'POST',
            url: '/player/server_status.php', 
            data: {"server_cdn": min[0]},
            async: true,
            success: function() { }
        }); 

    });
</script>

My javascript will load a .json file and verify some factors, after this, will get a data from external URL and call server_status.php file with a $_POST data, the script will detect that $_POST exists and will create a file on server-side. Everything is working on my computer, but crontab is not creating the file at server-side.

I guess that the problem is not related to PHP, only Javascript, I don't know what is exactly happening. Can you help me solve this issue?

Thank you very much.

Streaz
  • 11
  • 1
  • I may not understand the question properly, But are you expecting javascript to be ran by the cron job ? Javascript will be ran by the browser, Cron will only call the page not run the actual javascript on there. – Nicolas Racine Mar 26 '19 at 04:37
  • You are trying to run script from command line which contains JavaScript code in it. JavaScript runs only on browser not from terminal with curl. – SachinPatil4991 Mar 26 '19 at 04:38
  • So what should I do to run javascript on crontab? – Streaz Mar 26 '19 at 04:40
  • why aren't you running everything server side? you would make it much more reliable this way. have a look at this (very similar) problem and answer: https://stackoverflow.com/questions/25515936/perform-curl-request-in-javascript – Niels Mar 26 '19 at 04:48
  • Because I'm not able to write a file on server using javascript – Streaz Mar 26 '19 at 05:04

1 Answers1

0

Using browsers like SlimerJS or PhantomJS is your way to do that, you will be able to execute Javascript on server side.

user2203703
  • 1,955
  • 4
  • 22
  • 36