Showing posts with label cronjob; crontab; UNIX. Show all posts
Showing posts with label cronjob; crontab; UNIX. Show all posts

Monday, July 12, 2021

Give user access to crontab; UNIX

https://www.thegeekdiary.com/how-to-allow-only-specific-non-root-users-to-use-crontab/

 

Add username to /etc/cron.allow as root user.

Thursday, August 20, 2020

Shell script to spwan sftp process; cronjob; UNIX

Below script can be scheduled to run every 5 mins to fetch files remote to local server.

Crantab -e would allow you to schedule jobs on LINUX servers. 


#!/bin/bash

/usr/bin/expect << EOF
spawn sftp USERNAME@host
expect "Connecting to 127.0.0.1..."
expect "password:"
send "Enter Credential\r"
expect "sftp>"
send "cd Test\r"
expect "sftp>"
send "lcd /home/username/App-Files\r"
expect "sftp>"
send "mget * \r"
expect "sftp>"
send "rm * \r"
expect "sftp>"
send "quit \r"
EOF

Friday, October 20, 2017

How to create Cron jobs UNIX/LINUX

$crontab -l
//shows/prints existing cronjob

$crontab -e
//create a cronjob

$crontab -r 
//removes all cronjobs



below code writes test********** to testCronLog file every 1 min


  • */1 * * * * echo "test***********" >>/home/app/testCronLog
--below runs every 3 mins and prints output to appLog file
  • */3 * * * * /home/app/startProcess.sh >>/home/app/appLog




Actually, it's not recommended to handle those files by hand. Per crontab man page:
Each user can have their own crontab, and though
these are files in /var/spool/cron/crontabs, they are not
intended to be edited directly.
Files under /var/spool are considered temporary/working, that's why they probably get deleted during an upgrade, though a closer look at the cron package's upgrade scripts may shed some light on this.
Anyway, it's always a good practice to back up your cron entries or keep them in a file in your home directory.
I assume you're using crontab -e to create crontab files on the fly. If so, you can get a "copy" of your crontab file by doing crontab -l. Pipe that to a file to get a "backup":
crontab -l > my-crontab
Then you can edit that my-crontab file to add or modify entries, and then "install" it by giving it to crontab:
crontab my-crontab
This does the same syntax checking as crontab -e.



Reference:
https://docs.oracle.com/cd/E23824_01/html/821-1451/sysrescron-24589.html
https://www.taniarascia.com/setting-up-a-basic-cron-job-in-linux/
https://askubuntu.com/questions/216692/where-is-the-user-crontab-stored
https://kb.iu.edu/d/afiz