php exec crontab not working

北城以北 提交于 2021-01-28 02:22:30

问题


I trying on my local CentOS 5.3 server, when I try to execute below line in my php code from apache webserver

exec("crontab -l", $output, $arg);  
The $arg reply 127  (command not found code)

Then I changed below code

exec("/usr/bin/crontab -l", $output, $arg); 
The $arg reply 126  (Permission problem code)

Here is my current permission of /usr/bin/crontab -rwsr-sr-x 1 root root 311288 Mar 15 2007 /usr/bin/crontab.
And there is no /etc/cron.allow and /etc/cron.deny file.

I already turned off Safe_mode in php. Any suggestion to solve this problem?

////////////////////////////////////////////////////////////////////////////////

I note that my problem is related to selinux. It is working, when I disable selinux

echo 0 > /selinux/enforce

But I don't want to disable selinux, is there any option in selinux to allow crontab from apache?


回答1:


  1. Add /usr/bin to your shell env path
  2. Add crontab to the same group like of the webserver
  3. Try in php exec("echo $PATH") to see the path variable. You can also look for unsetenv and temporarly disable it

You can look in the php error log for a detailed description of the error but as you said you are familiar with Linux I can't teach you about user right management. A note to the env variable and the path variable it's not always sure the env variable get exported when you run a program. This is maybe the case with your problem. It means either the os is broken or you have to attach the env variable manually to your program. A workaround can be to look where the shell is looking for programs and symlink the program. In your error log it seems that crontab is in the folder /usr/bin. Also this seems not to be a programming question. Ask at server.stackexchange why crontab isn't found. For example my env looks like this:

LC_PAPER=de_DE LC_ADDRESS=de_DE LC_MONETARY=de_DE AF_PIDDIR=/tmp/af-piddir UPNP_ROOT=upnpav:// SHELL=/bin/sh TERM=xterm DISABLE_GATEWAY=1 GTK2_RC_FILES=/etc/hildon/theme/gtk-2.0/gtkrc TMPDIR=/var/tmp MMC_SWAP_LOCATION=/media/mmc1 LC_NUMERIC=de_DE MMC_MOUNTPOINT=/media/mmc1 OLDPWD=/root HILDON_FM_OBEX_ROOT=obex:// LAUNCHWRAPPER_NICE_KILL=/etc/osso-af-init/nice-kill-launch-wrapper.sh INTERNAL_MMC_MOUNTPOINT=/home/user/MyDocs OSSO_VERSION=RX-51_2009SE_20.2010.36-2_PR_MR0 USER=root LS_COLORS=no=00:fi=00:di=00;36:ln=00;35:pi=40;33:so=01;35:bd=40;32;00:cd=40;32;00:or=01;05;37;41:mi=01;05;37;41:ex=00;33:.cmd=00;32:.exe=00;32:.com=00;32:.btm=00;32:.bat=00;32:.sh=00;32:.csh=00;32:.tar=00;31:.tgz=00;31:.arj=00;31:.taz=00;31:.lzh=00;31:.zip=00;31:.z=00;31:.Z=00;31:.gz=00;31:.bz2=00;31:.bz=00;31:.tz=00;31:.rpm=00;31:.cpio=00;31:.jpg=00;35:.gif=00;35:.bmp=00;35:.xbm=00;35:.xpm=00;35:.png=00;35:.tif=00;35: LC_TELEPHONE=de_DE SESSION_BUS_PID_FILE=/tmp/dbus_session_bus.pid.user OSSO_PRODUCT_HARDWARE=RX-51 STATESAVEDIR=/tmp/osso-appl-states LAUNCHWRAPPER_NICE_TRYRESTART=/etc/osso-af-init/nice-launch-wrapper-tryrestart.sh OSSO_PRODUCT_FULL_NAME=Nokia N900 OSSO_PRODUCT_RELEASE_FULL_NAME=Maemo 5 OSSO_PRODUCT_RELEASE_NAME=Maemo 5 MYDOCSDIR=/home/user/MyDocs PATH=/usr/bin/gnu:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin LC_MESSAGES=de_DE OSSO_PRODUCT_SHORT_NAME=Nokia N900 SESSION_BUS_ADDRESS_FILE=/tmp/session_bus_address.user LC_IDENTIFICATION=de_DE PWD=/home/user/MyDocs EDITOR=/usr/bin/nano -w -c LANG=de_DE ERR=0 LC_MEASUREMENT=de_DE PS1=[\033[1;36m][[\033[0;36m]\u\h[\033[0;37m]:\w[\033[1;36m]][\033[m]$ [\033[0;37;00m] ILLEGAL_FAT_CHARS=/:*?<>| OSSO_PRODUCT_KEYBOARD=German AF_DEFINES_SOURCED=1 SHLVL=1 HOME=/root SDL_AUDIODRIVER=pulse OSSO_SWAP=/home/user/MyDocs OSSO_PRODUCT_RELEASE_VERSION=20.2010.36-2 LS_OPTIONS=--color LAUNCHWRAPPER_TRYRESTART=/etc/osso-af-init/launch-wrapper-tryrestart.sh OSSO_PRODUCT_NAME=N900 AF_INIT_DIR=/etc/osso-af-init MMC_DEVICE_FILE=/dev/mmcblk0p1 LOGNAME=root DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-eCbOI2qrVP,guid=cd1dea104334a338a9007b9700000014 LAUNCHWRAPPER=/etc/osso-af-init/launch-wrapper.sh OSSO_PRODUCT_WLAN_CHANNEL=fcc/us MAX_FILENAME_LENGTH=255 DISPLAY=:0.0 LAUNCHWRAPPER_NICE=/etc/osso-af-init/nice-launch-wrapper.sh LC_TIME=de_DE INTERNAL_MMC_SWAP_LOCATION=/home/user/MyDocs LC_NAME=de_DE OSSO_PRODUCT_REGION=Germany _=/usr/bin/gnu/env

Just type env in the prompt.



来源:https://stackoverflow.com/questions/7884450/php-exec-crontab-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!