Change server CRON function via PHP

Asked

Viewed 298 times

2

You can change any 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 insert that he 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

That’s possible?

  • Yes it is possible, you can use the shell_exec (very carefully, this is something I would not do) to run the crontab commands (crontab -e) and manage according to what each user needs. One question, who is your cloud provider? The vast majority already have better solutions for this...

  • I work with an internal company server, I could not do otherwise

  • Okay, have you tried shell_exec? Just be careful what you pass to this function, they might try injecting malicious code into the request... You have some tutorials on this: https://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428

1 answer

1


It is possible yes.

Create a bash script, for example in /etc/scripts/meu_script to make that kind 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

permission to run and add the script to the /etc/sudoers file. Note that you even being root will not have permission to edit it, so before editing it change the file’s permission

chmod 775 /etc/sudoers

Make the following edition

#
# 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 finishing editing it, go back to the permissions the file was in

chmod 440 /etc/sudoers

Now in your code PHP you can call your script bash without worrying about safety when using shell_exec in the PHP 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.