We are happy to announce that Vesta is back under active development as of 25 February 2024. We are working on v1 candidate and expect to engage more with the community over the coming months. We are committed to open source, and we encourage contributors to help us build the future of Vesta.
Adding cron job from a PHP script.
Adding cron job from a PHP script.
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.
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.
I think I can able to add/delete crons via API. I will check it:
But still my first question valid. By the way how can I add my signature?
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);
Last edited by mkaand on Fri Jan 12, 2018 12:28 pm, edited 1 time in total.
Re: Adding cron job from a PHP script.
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.
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.
Sorry for my posts, but I am trying to fix my problem. I need help.
I created a PHP gile called cron.php
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
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);
?>
I copied all inc folder (/usr/local/vesta/web) to public_html
Re: Adding cron job from a PHP script.
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.
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.
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.
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)
DELETE A CRON JOB:
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";
}
?>
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.
Hello,
How can is suspend the Cronjob with api?
if i try to run this command:
it suspend all cron jobs..
Regards,
Gabor Raska
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.
);
Regards,
Gabor Raska