Score:0

Cron Tab as specific user and python virtual env

us flag

Wanting to run an hourly cron job as a specific user (xfeautomation) and python virtual environment. Running as a specific user, will I have to add the script location into PATH? My current cron job looks like this:

0 * * * *  xfeautomation /opt/scripts/wf_venv/bin/python /opt/scripts/wf_api_linux.py 2>&l /opt/scripts/log.txt

Is this the correct or optimal way? TIA

Score:1
ng flag

switch to that user.

sudo su - xfeautomation

Run

crontab -e

A file editor with empty file will appear. Add your job without user name there and save it.

0 * * * * /opt/scripts/wf_venv/bin/python /opt/scripts/wf_api_linux.py 2>&l /opt/scripts/log.txt

Verification:

sudo su - xfeautomation crontab -l

OR

sudo crontab -u xfeautomation

Score:0
ar flag
bob

Running as a specific user, will I have to add the script location into PATH?

You usually don't need to modify the PATH environment variable when you're using absolute paths such as you're doing now with /opt/scripts/wf_venv/bin/python /opt/scripts/wf_api_linux.py 2>&l /opt/scripts/log.txt

As sysadmin you have several options to create scheduled batch jobs that run under a different user ID:

  1. As explained in this answer: set up a personal crontab for that user. Jobs in a personal crontab will always run under the user ID of their owner.
    Note that when you as the administrator set up a personal crontab for another user, that user will be able to modify and delete that cron job specification, undoing your work.

  2. Alternatively you can schedule a system job from the system crontab. The system crontab (typically /etc/crontab and/or drop-in files in /etc/cron.d/) is owned by root and can't be modified by unprivileged users. The job specification in the system crontab supports an extra field not present in personal crontab files: field #6 needs a username, such as xfeautomation of the user ID that will be used to execute the job.
    The syntax you posted is correct for an entry in /etc/crontab or a drop-in file like /etc/cron.d/wf_api_linux

    0 * * * *  xfeautomation /opt/scripts/wf_venv/bin/python /opt/scripts/wf_api_linux.py 2>&l /opt/scripts/log.txt
    

Since you're logging all output and errors not a concern, but normally cron will email standard output and errors to root when you set up a system crontab and directly to the user when using a personal crontab.

As far as I know effectively both methods result in the same conditions for the job and aside from the concerns about ownership of the job spec and emails, they are equivalent.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.