1

I am beginner in programming and I was trying to set up Cron Job using PHP script in couple ways, which I found on the internet, but I always run into some errors I can't fix by myself: First method:

 exec('echo -e "`crontab -l`* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php" | crontab -");

,but I think error appears because of ` symbol and I am sure a lot of ' symbols os not good either.

Second method:

 echo "* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php" | tee -a /var/spool/cron/root

Error is: ( ! ) Parse error: syntax error, unexpected 'var' (T_VAR) in /home/jharvard/vhosts/pset7/public/calendar.php on line 24

Third try:

 $cron_file = "{$_POST['lastname']}{$_POST['firstname']}";
// Create the file
touch($cron_file); 
// Make it writable
chmod($cron_file, 0777); 
// Save the cron
  file_put_contents($cron_file, "* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php"); 
 //Install the cron
exec('crontab cron_file');

I have no idea why this one doesn't work. I execute

crontab -l

, but it doesn't show any new jobs

Laurynas G
  • 573
  • 9
  • 30

2 Answers2

1

Your first try got lots of problem:

  • The string has started with single quotation, ended with double.
  • variables inside strings that started with double quote will be parsed. so if you start your string with single quote, variable won't replace with their values.
  • you can not use single quote character within a string started with single quote, unless you escape with backslash. (same apply to double quote as well) example: $var = '\'test\''

So grammatically, your first try should be like this:

exec("echo -e \"`crontab -l`* * {$post['birthDay']} {$post['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$post['lastname']}{$post['firstname']}.php\" | crontab -");

Better Solution

As I understand from your code, you want to do something on user's birthday. A better solution is create one single cronjob manually, that will run one file every night, and that file will manage what to do (including who's birthday is that, sending them email, etc) instead of having a cronjob for every user.

‌‌R‌‌‌.
  • 2,818
  • 26
  • 37
  • This gets my vote for the "another solution" bit. Editing the crontab with the web app is not a very good idea. – alzee Dec 16 '15 at 11:44
  • This just a university project, so I just need to show that principle works. I was thinking to do it by setting reminder on current day couple minutes after reminder was created (not just after the reminder button is pressed). I could try to set up this cron job manually to check for files every minute, but I wouldn't know how the second file should look like – Laurynas G Dec 16 '15 at 12:48
  • managed to do cronjob running every minute. Thanks :) – Laurynas G Dec 16 '15 at 14:42
0

You can do it like this

$command = " * * * * * php PATH_TO_YOUR/some.php ";
exec('echo -e "`crontab -l`\n'.$command.'" | crontab -', $result);
var_dump($result);

by this you can add to www-data's user crontab new command, which will run some.php every minute

for more information here is reference about crontab - http://www.adminschoice.com/crontab-quick-reference , you need to read it to understand what to do with * * * * * (which are responsible for cronjob running time) and how to build correct $command with usage of php,sh and other scripts

Armen
  • 4,064
  • 2
  • 23
  • 40
  • It executes this or my third method, but there is no job added to crontab, when I check "crontab -l" – Laurynas G Dec 16 '15 at 11:34
  • p.s. I think I understand * * * * * as I read quite a bit about it – Laurynas G Dec 16 '15 at 11:37
  • Are you checking it under `www-data` user ? try to `su www-data` and then `crontab -l` check what you get – Armen Dec 16 '15 at 11:38
  • `sudo crontab -u www-data -e` doesn't show any jobs either – Laurynas G Dec 16 '15 at 11:57
  • to debug it my suggestion is to first do it manually and see if all is ok, **1)** before `exec('echo -e ...` make `var_dump( 'echo -e "`crontab -l`\n'.$command.'" | crontab -' )` **2)** open terminal run `sudo su`, `su www-data` , `crontab -e` (it will suggest you to choose some editor choose vim or nano), then exit from ctontab file and run var_dumped in first step command in terminal and check again `crontab -l` – Armen Dec 16 '15 at 12:03
  • can it be a problem that after I enter `sudo su`, entering line becomes `root@appliance:/home/jharvard#`, but after I enter `su www-data` it says `This account is currently not available.`? – Laurynas G Dec 16 '15 at 12:39
  • That `www-data` should be linux user which your `php` uses probably on your pc it is different user try `` in php and check which user your php uses (it is important to run it from php to see your php user), we are checking this to understand for which user your php create crontab information (when you run `exec('echo -e "`crontab` it add that info for php user), in linux each user can have its separate crontab and – Armen Dec 16 '15 at 12:50