Crontab without crontab -e - cron

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.

Related

Concatenate file output text with Crontab

I have followed up this question successfully, Using CRON jobs to visit url?, to maintain the following Cron task:
*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=SOME_ACCESS_TOKEN_HERE >/dev/null 2>&1
The above Cron task works fine and it visits the URL periodically every 30 minutes.
However, the access token is recorded in a text file found in /home/myaccount/www/site/aToken.txt, the aToken file is very simple text file of one line which just contains the token string.
I have tried to read its contents and pass, using cat, it to the crontab command like the following:
*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=|cat /home/myaccount/www/site/aToken.txt| >/dev/null 2>&1
However, the above solution has been failed to run the cronjob.
I edit Cronjobs using crontab -e using nano on Ubuntu 16.04
This is a quick solution that will do exactly what you want without the complicated one-liner:
Create this file in your myaccount -- You may also put it into your bin directory if you so desire just remember where you put it so you can call it from your CRON. Also make sure the user has permissions to read/write to the directory the sh file is in
wget.sh
#!/bin/bash
#simple cd -- change directory
cd /home/myaccount/www/site/
#grab token into variable aToken
aToken=`cat aToken.txt`
#simple cd -- move to wget directory
cd /wherever/you/want/the/wget/results/saved
#Notice the $ -- This is how we let the shell know that aToken is a variable = $aToken
#wget -O - https://example.com/operation/lazy-actions?lt=$aToken
wget -q -nv -O /tmp/wget.txt https://example.com/operation/lazy-actions?lt=$aToken >/dev/null 2>/dev/null
# You can writle logs etc etc afterward here. IE
echo "Job was successful" >> /dir/to/logs/success.log
Then simply call this file with your CRON like you are doing already.
*/30 * * * * sh /home/myaccount/www/site/wget.sh >/dev/null 2>&1
Built on that question, Concatenate in bash the output of two commands without newline character, I have got the following simple solution:
wget -O - https://example.com/operation/lazy-actions?lt="$(cat /home/myaccount/www/site/aToken.txt)" >/dev/null 2>&1
Where it able to read the contents of the text file and then echoed to the command stream.

Including Cron/Crontab in my Bash Backup Script with inputs

Im doing a Bash-Backup Script with 3 Options:
Do fullbackup
Do fullbackup at a specific time with cron
First I want to ask for the path like: Path of directory: /home
Then i want the hour for the backup: Hour for the backup (0:00-23:59) : 2:00
Then a simple question like: The backup will execute at 2:00. Do you agree(y/n)
I know how to do a crontab but I have no idea how to include that in my script so that I choose option 2 and then the script asks me for the directory and time and set then the crontab.
Any ideas or help would be appreciated!
You can get the existing crontab with
crontab -l
and install a new crontab with
crontab file
So your script would probably need something like
crontab -l | grep -v "# install-backup-script" > /tmp/file.$$
echo "$min $hour * * * /full/path/to/script # install-backup-script" >> /tmp/file.$$
crontab /tmp/file.$$
rm -f /tmp/file.$$

crontab suggested error :/bin/sh .backup.sh no such file or directory

I use the command crontab -e to execute a shell script at regular intervals.
But something wrong with it.
The shell script is located in /opt/oneinstack/oneinstack,so my crontab is :
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log
2>&1 &
The permission for the backup.sh file is 755.
The user using the crontab -e command is root
Message in /data/back.log is /bin/sh no such file or directory .Just like the title.
Manual execution the backup.sh is no problem.
What's wrong with it? And how can I fixed it?
Expect someone to help me.Thanks a lot!!
Try setting the following before the crontab list. For example, if this line is at the top:
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log 2>&1 &
then it looks like this:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log 2>&1 &
In addition:
Like #Ugur mentioned, it is unnecessary to cd, if you take care of the paths correctly in the backup.sh . For example:
ROOTPATH="/path/to/data"
TARTGETPATH="/path/to/target"
FileToBackup="${ROOTPATH}/myfile"
rsync $FileToBackup $TARGETPATH/

Crontab - simple echo not running

I've got such situation:
I want to schedule a job with crontab on a linux server. I'm not super-user, so I'm editing (with crontab -l, editor vim) only my crontab file. For testing, I put there:
* * * * * echo asdf
And the job is not running. Is the restart of the server needed? Or maybe some administrator move?
May be it is, cron jobs will run in their own shell. So you can't expect to see asdf on your console.
What you should try is
* * * * * echo asdf > somefile_in_your_home_directory_with_complete_path.log
Next check the file by doing a tail:
tail -f somefile_in_your_home_directory_with_complete_path.log
And if it's not, check if the cron daemon itself is running or is down:
# pgrep crond
OR
# service crond status
If you want to echo something on your shell you could use wall:
* * * * * wall <<< "Hello from cron"
* * * * * echo "Hello from cron" | wall
These two lines basically do the same but the first one might not work on older shell, just choose your favorite.
Anyway, be aware that wall will send your message to every user currently connected.
For me * * * * * /bin/echo text > file is not working...I don't know why, previleges and everything is set.
(This command is running normaly when I execute it as the particular
root user, just to clarify this.)
This can be solved by injecting the path PATH=$PATH:/bin in my example.
Instead * * * * * echo text > file is working fine, probably path issue.
Hope I helped

How to schedule two separate crontabs

It supposed to be simple, but I must be missing something.
deletelogs file:
0 11 * * 6 ./scripts/deletelogs.sh
backupstuff file:
0 23 * * * ./scripts/backupstuff.sh
crontab -l shows the deletelogs job
If I do crontab backupstuff, then crontab -l shows the backupstuff job.
How can I schedule both, Why can't I list both with crontab -l?
The crontab file can read a set of entries from a file named on its command line:
crontab filename
or from standard input:
echo ... | crontab
Both commands replace the entire crontab.
To combine two input files:
cat file1 file2 | crontab
To add the contents of a file to an existing crontab:
( crontab -l ; cat file ) | crontab
Personally, I keep my entire crontab in a single file, installed with a simple crontab filename; I keep that file under a source control system so I can restore it if I make a mistake. But if you have a need to split it into multiple files, you can do that.

Resources