Should linux cron jobs be specified with an & to indicate to run in background?
Every job that's run by cron is run in the background automatically, so no need for the &
See this too.
Linux: Run cron job in foreground
Try this out on your user's crontab
:
@hourly DISPLAY=:0 xterm -e /path/to/my/script.sh
It will open (hourly) an xterm
with your script executing, and exit after your script exits. Of course, you should modify the @hourly
part to suit your needs.
How to constantly run Python script in the background on Windows?
On Windows, you can use pythonw.exe
in order to run a python script as a background process:
Python scripts (files with the extension
.py
) will be executed bypython.exe
by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension.pyw
which will cause the script to be
executed bypythonw.exe
by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
For example,
C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py
In order to make your script run continuously, you can use sched
for event scheduling:
The sched module defines a class which implements a general purpose
event scheduler
import sched
import time
event_schedule = sched.scheduler(time.time, time.sleep)
def do_something():
print("Hello, World!")
event_schedule.enter(30, 1, do_something, (sc,))
event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()
Now in order to kill a background process on Windows, you simply need to run:
taskkill /pid processId /f
Where processId
is the ID of the process you want to kill.
CronJob not running
Finally I found the solution. Following is the solution:-
Never use relative path in python scripts to be executed via crontab.
I did something like this instead:-import os
import sys
import time, datetime
CLASS_PATH = '/srv/www/live/mainapp/classes'
SETTINGS_PATH = '/srv/www/live/foodtrade'
sys.path.insert(0, CLASS_PATH)
sys.path.insert(1,SETTINGS_PATH)
import other_py_filesNever supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.
Run CRON job everyday at specific time
Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
Example::Scheduling a Job For a Specific Time
The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use
8, and for 8 PM use 20.
30 08 10 06 * /home/yourname/full-backup
- 30 – 30th Minute
- 08 – 08 AM
- 10 – 10th Day
- 06 – 6th Month (June)
- *– Every day of the week
In your case, for 2.30PM
,
30 14 * * * YOURCMD
- 30 – 30th Minute
- 14 – 2PM
- *– Every day
- *– Every month
- *– Every day of the week
To know more about cron, visit this website.
What is preventing my cron job from running
sudo
The sudo command may not work in a crontab. Generally you need a password to run sudo but there might be a way to have it run without a password when running in a cron job. This would not be recommended however to attempt.
cron
You'll need to run the cron as a user that has access to do what you need to accomplish. Cron runs with a short list of specific paths. By default that list is pretty short. On a linux box I use the path is /sbin:/usr/sbin:/bin:/usr/bin
.
Also, the paths need to be more specific. Cron doesn't run as a normal user so you have to be more specific with paths and output of those commands.
For instance, on the first command, where will the gzip file be placed?
logrotate
It looks like you're trying to zip a log file, then move log files, then remove old log files - this is exactly what logrotate accomplishes. It would be worth installing. Logrotate solves problems like the log file being opened when you run this command - generally the process that has the log file opened doesn't lose the file handle even if you rename it so the log continues to be written to even after you move it. It also handles the problem of keeping an archive of the recent log files, like syslog.1.gz, syslog.2.gz, syslog.x.gz or as many back as you have storage space for or want to keep for posterity.
Summary
- Don't use sudo in cron
- Be specific in paths when running commands in cron
- Use logrotate to accomplish this specific task in your question
Related Topics
Bash Script Read All the Files in Directory
How to Make Debian Package Install Dependencies
Capturing Performance with Pcap VS Raw Socket
Doesn't Sh Support Process Substitution <(...)
How to Connect to a Terminal to a Serial-To-Usb Device on Ubuntu 10.10 (Maverick Meerkat)
Error: Gdal-Config Not Found While Installing R Dependent Packages Whereas Gdal Is Installed
How to Check the Version of Openmp on Linux
How Is the Microsecond Time of Linux Gettimeofday() Obtained and What Is Its Accuracy
/Proc/$Pid/Maps Shows Pages with No Rwx Permissions on X86_64 Linux
Installing Jenkins Plugins to Docker Jenkins
Using Linux How to Pass the Contents of a File as a Parameter to an Executable
Remove Log Files Using Cron Job
Linux Perf Reporting Cache Misses for Unexpected Instruction
Linux Kernel System Call Returns -1 Instead of {-1, -256}
Get Final Url After Curl Is Redirected
Adding Timestamp to a Filename with Mv in Bash
Fuzzy File Search in Linux Console
What's the Accepted Method for Deploying a Linux Application That Relies on Shared Libraries