Forums

console closes for strange reason

Hi,

I have a script that runs fine from bash, but when I try to schedule it, it does not work. log gives message: 2022-12-06 20:20:10 -- Completed task, took 5.01 seconds, return code was 0.

and: /usr/local/lib/python3.9/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access ./credential.json: No such file or directory warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

I don't understand how it is possible that it runs from bash, but not from task scheduler. Could it maybe be an issue with different environments?

cheers Thomas

Check the working directory in both cases.

I am not sury I understand. The file is running from a directory and the credential.json file is in the directory. When I run it from the task scheduler or from the bash, it is always the same file. How do I check working directory?

A scheduled task will run in your home directory, /home/thomaskruithof2, so if you ask Python to load the file ./credential.json, you are asking for the contents of /home/thomaskruithof2/credential.json.

The same would be true in a Bash console; if you started a new console, its working directory would be /home/thomaskruithof2/, so if you ran code from there to load a file specified in that way, then it would try to load it from the current working directory.

However, my guess is that when you run it from Bash, if you were in a new console, you would first use the cd command to change the working directory to the one containing the script (which I suspect also contains the credentials file). Then you would run the script from there.

You need to do the same thing in a scheduled task. You can separate separate Bash commands with semicolons. So, if in a freshly-started Bash console you would do this:

cd somedirectory
python somescript.py

...then your scheduled task would need to look like this:

cd somedirectory; python somescript.py

Thanks! That solves it,

cheers

Great, thanks for confirming that!