Change server CRON function through PHP

2

Is it possible to change some CRON function with PHP or any other language?

For example, I have the following non-server function:

30 15 * * 1-5 php /var/www/html/projetos/hist_06.php

In this application the user would enter that they would like the program to run at 14:00, then the command on the server would change to:

00 14 * * 1-5 php /var/www/html/projetos/hist_06.php

Is this possible?

    
asked by anonymous 29.09.2017 / 14:54

1 answer

1

Yes, it is possible.

Create a script in bash, for example in / etc / scripts / my_script to make this type of change example:

#!/bin/bash

min=$1
hra=$2
dia=$3
mes=$4
semana=$5

# Caso algum dos parâmetro seja nulo, seta a variável com "*"
if [[ -z $min ]]; then
    min='*'
fi
if [[ -z $har ]]; then
    hra='*'
fi
if [[ -z $dia ]]; then
    dia='*'
fi
if [[ -z $mes ]]; then
    mes='*'
fi
if [[ -z $semana ]]; then
    semana='*'
fi
# Gera o arquivo de cron com os parametro que foi passado quando o script foi executado
echo "$min $hra $dia $mes $semana $user $cmd" > /etc/cron.d/meu_cron
# Se foi bem sucedido retorna 0 ou 1
if [[ $? -eq 0 ]]; then
    /etc/init.d/cron restart
    echo 1
else
    echo 0
fi

Run permission and add the script to the / etc / sudoers file. Note that even though you are root you will not be allowed to edit it, so before you edit it, change the file's permission

chmod 775 /etc/sudoers

Make the following edit

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
**php ALL = NOPASSWD:/etc/scripts/meus_cript**

After you finish editing it, go back to the permissions that the file was

chmod 440 /etc/sudoers

Now in your PHP code you can call your bash script without worrying about security when using shell_exec in PHP strong> Run the script by passing the desired parameters

Example:

exec=('/usr/bin/sudo /etc/scripts/meu_script 00 14 * * 1-5 php /var/www/html/projetos/hist_06.php')


cat /etc/cron.d/meu_cron

00 14 * * 1-5 php /var/www/html/projetos/hist_06.php
    
06.10.2017 / 22:14