How can i open crontab file to add new task? - cron

firstly i copied crontab file into new file (samah-crontab) using these commands:
crontab -l > samah-crontab
crontab samah-crontab
sudo service cron restart
I tried to do this:
file = open("/home/pi/samah-crontab","a")
file.write("50 22 * * * sudo python /home/pi/ON.py")
file.close()
I want to open crontab to add new task without using this command:
crontab -e
Also i tried to open this file "I noticed it while doing crontab -e" in each time i do "crontab -e" there new id for the file appears !! As(wP4Rji)
file = open("/tmp/crontab.wP4Rji/crontab","a")
file.write("50 22 * * * sudo python /home/pi/ON.py")
file.close()
But nothing well!!!
i can't add new task without using "crontab -e". Why?
My idea is to add new task using python file and not as used to do this using edit command on terminal.
Any comment ?
Thanks

Related

How to make crontab run new version of my script

I have currently running python script on debian system. Now because of some reasons I changed this script and updated cron, but nothing has changed. Also, I tried to save this cron in different file and create new cron - line with job appears, but script doesn't work.
CRON[22310] (root) CMD ( /usr/bin/python /home/radmin/test/test.py)
from /etc/crontab for new script:
*/1 * * * * root /usr/bin/python /home/radmin/test/test.py
for old script:
*/1 * * * * root python /home/radmin/base.py
Script runs correctly without cron.
Tried restarting and reloading cron.
It looks like cron dosen't recognize the root command you add.
try instead opening cron using sudo crontab -e and then add your code:
*/1 * * * * /usr/bin/python /home/radmin/test/test.py
by opening it up with sudo it will add it to the root user cron jobs.
Problem was in python code. Crontab is okay. I'm using python lib "requests" and there is a method to get post request content - ".text", so this method doesn't want to work in cron (empty error logs while running) (still don't know why). So changing .text to .content solved this issue.

sh file not running on cron ubuntu

I am trying to run a shell script on crontab on Ubuntu platform. I have tried googling and other links but nothing has helped so far.
This is my crontab:
*/2 * * * * sudo bash /data/html/mysite/site_cleanup.sh
This is the content of my sh file:
#!/bin/sh
# How many days retention do we want ?
DAYS=0
# geting present day
now=$(date +"%m_%d_%Y")
# Where is the base directory
BASEDIR=/data/html/mysite
#where is the backup directory
BKPDIR=/data/html/backup
# Where is the log file
LOGFILE=$BKPDIR/log/mysite.log
# add to tar
tar -cvzf $now.tar.gz $BASEDIR
mv $now.tar.gz $BKPDIR
# REMOVE OLD FILES
echo `date` Purge Started >> $LOGFILE
find $BASEDIR -mtime +$DAYS | xargs rm
echo `date` Purge Completed >> $LOGFILE
The same script runs from a terminal and gives the desired result.
Generic troubleshooting for noninteractive shell scripts
Put set -x; exec 2>/path/to/logfile at the top of your script to log all subsequent commands to a file as they're run. If this doesn't work, you'll know that your script isn't being run at all; if it does, you'll know where it fails and how.
If this is a personal crontab
If you're running crontab -e as a user (without sudo), then the crontab being modified is one for commands run with that user's permissions. Check that file permissions allow that user to modify the content in question (which, if these files are in a cgi-bin directory, may require being run by the same user as the web server).
If your intent is to have commands run as root, rather than as your own user, be sure you use sudo when editing the crontab to edit the system crontab instead (but please take care as to your script's correctness in this case -- carelessness such as missing quotes or lack of appropriate precautions in xargs usage can cause a script to delete the wrong files if malicious filenames are created):
sudo crontab -e ## to edit the system (root) crontab
...or, if you're cleaning up files owned by the apache user (for example; check which account is correct for your own operating system and web server):
sudo -u apache crontab -e ## to edit the apache user's crontab
Troubleshooting for a system crontab
Do not attempt to put a sudo command within the commands run by cron; with sudo's default configuration, it requires a TTY (a keyboard and screen) to be attached to a session in order to run. Thus, your crontab line should not contain sudo, but instead should look like the following:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh
Your issue is likely coming from the sudo call from your user level cron. Unless you've gone through and edited the bashrc profile to allow that script to run without sudo it'll hang up every time.
So you can lookup how to run a script with no password by modifying the bashrc profile, remove the sudo call if you aren't doing something in your script that calls for Super User permissions, or as a last ditch, extremely bad idea you can call your script from root's cron by doing sudo crontab -e or sudo env EDITOR=nano crontab -e if you prefer nano as your editor.
try to add this line to the crontab of user root and without the sudo.
like this:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh

Downloading files with bash via cron

I have build a bash script that gets .tar.gz files from IMDb and writes to a log file, the script works when run on its own as I can see the folder with the files present, but when I run the script via cron it doesn't work. Would this be due to permissions? I have edited the sudo crontab file, but I'm not sure what else I need to do.
Try this solution:
Cronjob is a file that contains your job:
cat cronjob
* * * * * bash /path/to/script.sh >> /path/to/log.txt
Then you should set executable permission and start cron service:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
After that you should tell cron service to run cronjob file:
crontab cronjob
Your script should download a file.
If your script doesn't run you should run it from good path[full path], so your cronjob file would be something like this:
* * * * * /bin/bash /path/to/script.sh >> /path/to/log.txt

Modify directory crontab looks in for crontab file?

I have a crontab file at /etc/crontab. I didn't place it there, it was already there, along with a lot of other files in /etc. However, when I type the command crontab -e, it creates a new file. I want it to look for crontab in /etc. This is what the command line says when I type crontab -e.
no crontab for root - using an empty one
~
~
~
~
~
"/tmp/crontab.vx7AxR" 0L, 0C
After a complete edit, this is what I end up with. Any ideas?
[root#server ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
"/tmp/crontab.uJzKS0":1: bad day-of-month
errors in crontab file, can't install.
Do you want to retry the same edit?
Contents of the crontab file:
0 * * * * /var/www/html/myfile.py
Thanks, I appreciate it. I wish there was more info out there about customizing crontab.

Crontab without crontab -e

I would like to add a crontab schedule by doing this in my server:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > crontab -e
Is there a way to do this without going doing crontab -e and then typing in the command?
Could try
1)nano /etc/crontab (or any other editor, e.g. emacs)
2)echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /etc/crontab
3)or put the contents of this into a file, then do "file > /etc/crontab"
like root:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /var/spool/cron/crontabs/username
We have the following setup in production on RHEL:
- a custom software starting sh in init.d , which
- handles cron start , stop , reload
- writes cron tasks into separate tmp file and loads this file with crontab -e
I have been only maintaining it for several months but it works like a charm ...
The proper fix is probably to use a file as specified in https://stackoverflow.com/a/4421284/377927, but it is possible to use tee to append a line to the crontab by doing e.g.:
echo "* * * * * ls" | EDITOR="tee -a" crontab -e
tee -a will append stdin to the file it gets specfied, the EDITOR variable tells crontab to use tee -a as editor.
If you have the whole crontab in a text file, you can upload a whole crontab to replace the old crontab by doing:
cat <crontab_text_file> | crontab -
This will wipe out your old crontab. Using '-' allows you to use standard input into the crontab.

Resources