Python Forum
[SOLVED] [Linux] Run Python script through cron?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Linux] Run Python script through cron?
#1
Hello,

On a Debian host, I need to run a Python script through cron. The script will need to write a file elsewhere on the disk.

I have the following questions:
1. Will it run as root or as the script owner?
2. What access rights (chmod) should I use?
3. Do I need to set the shebang line? #!/usr/bin/python
4. In the cron file (crontab -e) , do I need to use absolute paths for both the interpreter and the script, eg. /usr/bin/python /home/joe/script.py
5. In case it doesn't work as expected, does cron write a log file somewhere?

Thank you.
Reply
#2
1. Will it run as a script owner? - depends what user executes the crontab - If you set crontab as root it will execute as root. If you set the crontab as a user it will execute as users. You can set it up to execute using sudo but, I don't remember how as it's been a while.

2. What access rights - needs to have execute, maybe read. Unless root also need write on the directory write the file

3. Do I need the shebang line> - Common practice if you have multiple versions of python. If not used will use default python version

4. Do I need absolute paths? - Yes

5. Does crontab have logs? - Most logs are in /var/log. It may log to syslog. You can check the /etc/crontab file and see how the log is setup
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
Worked perfect. Thanks for the help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,559 Oct-17-2024, 01:15 AM
Last Post: Winfried
  Linux, Python, C ++, Brain2 and errors. PiotrBujakowski 0 984 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  Is possible to run the python command to call python script on linux? cuten222 6 2,370 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  ModuleNotFoundError only if script is launched by cron Charles33 8 7,787 Jan-12-2024, 12:55 AM
Last Post: caicaifan
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 8,125 Jun-29-2023, 11:57 AM
Last Post: gologica
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 1,758 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 1,598 Nov-16-2022, 07:58 PM
Last Post: Winfried
  How to compile a Python script for a Windows / Linux executable? netanelst 2 2,128 May-24-2022, 07:02 AM
Last Post: netanelst
  How to use a variable in linux command in python code? ilknurg 2 2,406 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Python syntax in Linux St0rmcr0w 2 66,293 Jul-29-2021, 01:40 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020