Cron Is Running in Home Directory Instead of File Directory

Cron is running in home directory instead of file directory

By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.

Consider changing your crontab to:

* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log

Good luck!

Crontab - Run in directory

All jobs are executed by a shell, so start that shell snippet by a command to change the directory.

cd /path/to/directory && ./bin/myapp

Concerning the use of && instead of ;: normally it doesn't make a difference, but if the cd command fails (e.g. because the directory doesn't exist) with && the application isn't executed, whereas with ; it's executed (but not in the intended directory).

Running Cron Job, file put into root directory / not were the php script is running from

Either specify the full path

$xmlfile = __DIR__ . '/sitemap.xml';
file_put_contents($xmlfile, $xmlsitemap);

or change working directories first

chdir(__DIR__);
$xmlfile = 'sitemap.xml';
file_put_contents($xmlfile, $xmlsitemap);

Note, __DIR__ means the path to the folder that contains the script that is currently being executed. You might prefer to specify the whole path instead, but they appear to be one and the same.

disable crontab auto generate file in home directory

Those files are what you downloaded using that wget command. wget queries a url and saves the response as a file. You asked it to. If, as typical for usage within a cron job, you are not interested in the response you must tell the system to ignore the response, to throw it away:

*/5 * * * * wget --post-data="pass=1123&Type=111" http://test.biz/index.php/game_line 2>&1 1>/dev/null

Cron not running Python script if script not in home folder

cron is running crontab(5) entries from the home directory of the user.

You need to change appropriately the directory i.e. to call the chdir(2) syscall (either thru the cd shell builtin, or inside your python script using os.chdir).

You should query the current directory (using getcwd(3), or the pwd command, or the os.getcwd in Python) in your script.

Also check your PATH if running commands.

Cron job in Ubuntu

You have not provided any path for test.log so it will be created in the current path(which is the home directory of the user by default).
You should update your script and provide the full path, e.g:

echo "$(date): cron job run" >> /var/log/test.log

PHP - Auto detecting home directory through cron job

I think you're looking for getcwd(). It returns the current working directory.

Python script run via cron job returning IOError [error no2]

Cron sets a minimal environment for the jobs (and I think it runs the job from the home directory).

Inside your python script, when you do something like -

open('<filename>')

It checks for the filename in the current working directory , not the directory in which your scripts exist.

That is true even when running from commandline , if you change directory to some other directory (maybe your home directory) , and then use absolute path to your script to run it, you should be getting the same error.

Instead of depending on the current working directory to be correct and have the file you want to open, you can try either of the below options -

  1. Use an absolute paths to the files you want to open , do not use relative path.

  2. Or If the above is not an option for you, and the files you want to open are present relative to the script that is getting run (for example purpose lets say in the same directory) , then you can use __file__ (this gives the script location) and os.path , to create the absolute path to your file at runtime, Example -

    import os.path

    fdir = os.path.abspath(os.path.dirname(__file__)) #This would give the absolute path to the directory in which your script exists.
    f = os.path.join(fdir,'<yourfile')

At the end f would have the path to your file and you can use that to open your file.

Java job not writing to file when running under cron

The problem was that Java could not execute the C program "util_mem_usage", despite me having "su" commands dotted around.
My solution was to seperate this command into a shell scipt.
The paths were what are required for the C program to work (and probably what Java was silently struggling with too).

#!/bin/bash
PATH=/home/jcb/bin:/usr/bin/
**LD_LIBRARY_PATH=/home/jcb/lib
PROJECT_ROOT=/home/jcb
export LD_LIBRARY_PATH
export PROJECT_ROOT**
util_mem_usage | tee /home/jcb/bridge/heartbeat/hb_bridge_util_mem_usage.txt

My java program then call this instead of the C utility

processBuilder.command("bash", "-c", "/home/jcb/bridge/heartbeat/hb_get_bridge_util_mem_usage.sh");

It does not seem like an elegant solution, but it works...



Related Topics



Leave a reply



Submit