Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:13+00:00 2026-05-23T14:12:13+00:00

If I type crontab -l in the command-line I can see the following line:

  • 0

If I type crontab -l in the command-line I can see the following line:

# * * * * * /usr/bin/php /home/user/every_minute_script.php

To start this cronjob, I need to edit the file using crontab -e command, remove the comment character at the beginning of the line, save the edited file, and exit the editor.

To stop this cronjob, the same steps, but adding the comment character at the beginning of the line.

I want to achieve exactly the same effect using a PHP script, instead of manually editing the file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:12:14+00:00Added an answer on May 23, 2026 at 2:12 pm

    I did some research and found in a forum, the following message:

    Call “crontab -e” with the EDITOR
    environment variable set to a php
    script. That script can modify the
    file and when it exits crontab will
    re-read the file and update.

    So, I have tried something, and it worked. I will paste the working code below:

    #!/usr/bin/php
    <?php
    
    $on  = "* * * * * /usr/bin/php /home/user/every_minute_script.php\n";
    $off = "# * * * * * /usr/bin/php /home/user/every_minute_script.php\n";
    
    $param    = isset( $argv[1] ) ? $argv[1] : '';
    $filename = isset( $argv[2] ) ? $argv[2] : '';
    
    if ( $param == 'activate' )
    {
        shell_exec( 'export EDITOR="/home/user/cron.php on"; crontab -e' );
    }
    elseif( $param == 'deactivate' )
    {
        shell_exec( 'export EDITOR="/home/user/cron.php off"; crontab -e' );
    }
    elseif( in_array( $param, array( 'on', 'off' ) ) )
    {
        if ( !is_writable( $filename ) )
            exit();
    
        $crontab = file( $filename );
        $key = array_search( $param == 'on' ? $off : $on, $crontab );
    
        if ( $key === false )
            exit();
    
        $crontab[$key] = $param == 'on' ? $on : $off;
        sleep( 1 );
        file_put_contents( $filename, implode( '', $crontab ) );
    }
    
    exit();
    
    ?>
    

    As it is, we have a single script named cron.php located at /home/user folder, set to be executable (chmod a+x cron.php) and called from the command-line (PHP-CLI). Later I will tweak it to run from the web, which is my intent.

    Usage: ./cron.php activate to enable the cronjob and ./cron.php deactivate to disable it.

    The script sets the EDITOR environment variable properly (to itself) and then calls crontab -e, which on its turn calls the EDITOR (which is now the same cron.php script) passing the temporary crontab file location as an argument. Then, the proper crontab line is found and changed, and the modified version is saved, substituting the temporary file. When the script exits, crontab will update.

    This does exactly what I wanted, and fit my needs.

    The other answers are nice and may fit different needs and scenarios, and I want to thank everybody who contributed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I type in simply the following line: NSManagedObject ... But Xcode doesn't recognise NSManagedObject
The type of this function is function :: Num a => ([Char],a) -> ([Char],a)
Displaying Type here to ... until the user enters text into a TextBox is
What type do arithmetic operators (+ - / *) have in PHP? I have
Type-hints can make a huge improvement on execution time where reflection occurs many times.
type Tag(Kids) = member this.Kids = Kids static member (-) (this: Tag, that: list<obj>)
type SQLConn = val mutable private connection : string option member this.Connection with get()
Type t = typeof(bool); string typeName = t.Name; In this simple example, typeName would
Type erasure is supposed to erase all generic information... If this is the case
Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.