Page 1 of 1

Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 11:35 am
by mkaand
Hello,

I am newbie in this form. This is my first post. I research a lot but I couldn't find a way. I have couple of questions regarding CRONJOBS.

1. How can I define unlimited cronjob to a VestaCP user?
2. I should able to add/remove cronjobs via a PHP script. How can I do that? What I need? Looks like VestaCP crons are not same my system's crontabs (Ubuntu v16). I prefer to add/remove cronjobs to VestaCP crons. Because It gives me interface.

I have a project called www.botbittrex.com I designed a Auto Trade & Monitor Telegram Bot for crypto exchange Bittrex. If new user register to my system, I can add his data to MySQL but I should add cronjob for each user by manually. I don't want to do that. It should be automatic. For this reason I should able to add/remove cronjobs via PHP. Thanks for help.

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 11:41 am
by mkaand
I think I can able to add/delete crons via API. I will check it:

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/main.php');
// Define schedule
$min = '*';
$hour = '*';
$week = '*';
$month = '*';
$wday = '*';
$cmd = 'echo test';
// Add cron job
exec (VESTA_CMD."v-add-cron-job ".$user." ".$min." ".$hour." ".$day." ".$month."
".$wday." ".$cmd, $output, $return_var);

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/main.php');
// Define cron job id
$job = escapeshellarg($_GET['job']);
// Delete cron job
exec (VESTA_CMD."v-delete-cron-job ".$user." ".$job, $output, $return_var);
But still my first question valid. By the way how can I add my signature?

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 12:15 pm
by mkaand
Question 1: Fixed. I select the package and go to cron section. Just simple I click to eternity sign.

But I am still working on Question 2. I try to add/remove cron jobs via API but I couldn't achieve. I need help about it.

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 12:27 pm
by mkaand
Sorry for my posts, but I am trying to fix my problem. I need help.
I created a PHP gile called cron.php

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use
include ($_SERVER['DOCUMENT_ROOT'] . '/inc/main.php');
// Define schedule
$min = '*/10';
$hour = '*';
$week = '*';
$month = '*';
$wday = '*';
$cmd = 'echo test';

// Add cron job
exec (VESTA_CMD."v-add-cron-job ".$user." ".$min." ".$hour." ".$day." ".$month."
".$wday." ".$cmd, $output, $return_var);

?>
But when I run this code it redirects me to server_address/login/

I copied all inc folder (/usr/local/vesta/web) to public_html

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 1:27 pm
by mkaand
I fixed Question 2 too. I want to share the solution. Unfortunately VestaCP API examples are not enough. I found delete database sample on the internet and I modified the code to fix my problem. This website has very usefull API examples for VestaCP -> https://www.iserveradmin.com/vesta-web-api/

Following code easily creates new cronjob via PHP. I believe similar code can delete the cron job too.

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use

// Define schedule
$username = 'YOUR_VESTA_USERNAME';
$min = '*/10';
$hour = '*';
$week = '*';
$month = '*';
$wday = '*';
$cmd = 'echo test';
//$output, $return_var);
// Server credentials
$vst_hostname = 'YOUR_VESTA_HOSTNAME';
$vst_username = 'YOUR_VESTA_USERNAME';
$vst_password = 'YOUR_VESTA_PASSWORD';
$vst_returncode = 'yes';
$vst_command = 'v-add-cron-job';

// Prepare POST query
$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
    'arg1' => $username,
    'arg2' => $min,
    'arg3' => $hour,
    'arg4' => $week,
    'arg5' => $month,
    'arg6' => $wday,
    'arg7' => $cmd
);
$postdata = http_build_query($postvars);

// Send POST query via cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);

// Check result
if($answer == 0) {
    echo "Cron job added\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}
?>

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 2:23 pm
by mkaand
I am trying to get a list of my cron jobs, after that I should get JOB ID than I want to delete some cron jobs. But I have a problem. What's wrong with the following code? Status code 0 (means OK) but I cannot see any result. If I type v-list-cron-jobs admin on shell I cann see the list of crons. But following PHP cannot show. I need help thanks.

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use

// Define schedule
$username = 'admin';

//$output, $return_var);
// Server credentials
$vst_hostname = 'SERVER_ADDRESS';
$vst_username = 'admin';
$vst_password = 'PASSWORD';
$vst_returncode = 'yes';
$vst_command = 'v-list-cron-jobs';

// Prepare POST query
$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
    'arg1' => $username
);
$postdata = http_build_query($postvars);
//echo $postdata;
// Send POST query via cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
var_dump($answer);
echo $answer;

// Check result
if($answer == 0) {
    echo "Cron job added\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}
?>

Re: Adding cron job from a PHP script.

Posted: Fri Jan 12, 2018 3:02 pm
by mkaand
I found out what's wrong in my code. If I change $vst_returncode = 'yes'; to $vst_returncode = 'no'; I can able to get list of my crons. I already share adding cron job code. Here is the listing and deleting cronjob php codes:

Listing Cron Jobs: (arg2 is optional, I prefer JSON)

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use

// Define schedule
$username = 'admin';

//$output, $return_var);
// Server credentials
$vst_hostname = 'YOUR_SERVER_HOSTNAME';
$vst_username = 'admin';
$vst_password = 'YOUR_USER_PASS';
$vst_returncode = 'no'; // THIS IS THE MOST IMPORTANT THING
$vst_command = 'v-list-cron-jobs';

// Prepare POST query
$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
	'arg1' => $username,
	'arg2' => 'json' // THIS LINE OPTIONAL
);
$postdata = http_build_query($postvars);
//echo $postdata;
// Send POST query via cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
var_dump(json_decode($answer),TRUE); 
// Check result
if($answer == 0) {
    echo "Cron job added\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}
?>
DELETE A CRON JOB:

Code: Select all

<?php
// Get environment variables and protect resource for unauthorized use

// Define schedule
$username = 'admin';

//$output, $return_var);
// Server credentials
$vst_hostname = 'YOUR_SERVER_HOSTNAME';
$vst_username = 'admin';
$vst_password = 'YOUR_PASSWORD';
$vst_returncode = 'yes';
$vst_command = 'v-delete-cron-job';

// Prepare POST query
$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
	'arg1' => $username,
	'arg2' => 31	// WHICH CRONJOB DO YOU WANT TO DELETE? CRONJOB ID COMES HERE.
);
$postdata = http_build_query($postvars);
//echo $postdata;
// Send POST query via cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);

// Check result
if($answer == 0) {
    echo "Cron job added\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}
?>

Re: Adding cron job from a PHP script.

Posted: Wed Apr 11, 2018 8:54 am
by lokutus
Hello,

How can is suspend the Cronjob with api?


if i try to run this command:

Code: Select all

$vst_command = 'v-suspend-cron-jobs';

// Prepare POST query
$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
    'arg1' => $username,
    'arg2' => 5    // WHICH CRONJOB DO YOU WANT TO DELETE? CRONJOB ID COMES HERE.
);
it suspend all cron jobs..
Regards,
Gabor Raska