I run a python shell from crontab every minute:
* * * * * /home/udi/foo/bar.py
/home/udi/foo has some necessary subdirectories, like /home/udi/foo/log and /home/udi/foo/config, which /home/udi/foo/bar.py refers to.
The problem is that crontab runs the script from a different working directory, so trying to open ./log/bar.log fails.
Is there a nice way to tell the script to change the working directory to the script's own directory? I would fancy a solution that would work for any script location, rather than explicitly telling the script where it is.




crontabuse-case: bothsys.argv[0]and__file__fail if script is run usingexecfile();inspect-based solution could be used instead.