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