Visit Our UNIX and Linux User Community

Add TimeStamp to cron job and write file name sent

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Add TimeStamp to cron job and write file name sent
# 1  
Old 02-23-2018
Display Add TimeStamp to cron job and write file name sent

I have written a cron job to automate the sftp of files using key authentication.
I wanted to add a timeStamp and name of file sent to a log file and append each these details to the same file each time files are sent and if possible include whether the files were sent successfully or not.

My ubuntu details:

Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial

Here is all the code i have so far:

cron job:

#01 17 * * * /etc/cron.daily/ 2>&1 | etc/cron.daily/ >> /home/user/sftpLogfile.log

#!/bin/bash -x
sftp -P 2224 -oIdentityFile=~/.ssh/id_rsa/sftp -v remoteMachine@IP_address <<EOF
cd /
lcd /sftpusers/transfers/sftp/data
put *.CSV*
[[ ${?} -eq 0 ]] && { echo "File transfer has been completed"; } || { echo "File transfer Failed"; }

while read x; do
    echo -n `date +%d/%m/%Y\ %H:%M:%S`;
    echo -n " ";
    echo $x;

My timestamp isn't working and doesn't output anything to a logFile although it did create the file.

How can I get the timeStamp to add to the logFile and also if possible the name of the file sent with the bool of successful or not?
Many thanks.

Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags as required by forum rules!

Last edited by RudiC; 02-23-2018 at 06:08 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 02-23-2018
Originally Posted by KidKoder
. . .

#01 17 * * * /etc/cron.daily/ 2>&1 | etc/cron.daily/ >> /home/user/sftpLogfile.log

. . .
Does etc/cron.daily/ (relative path) exist or is a / missing?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-23-2018
Yes the / was missing, thank you.

Any idea how to include the names of the files sent via sftp in my logfile?
# 4  
Old 02-23-2018
Did you consider raising the log level with -v?

Previous Thread | Next Thread
Test Your Knowledge in Computers #953
Difficulty: Medium
A memory-resident virus installs itself as part of the operating system when executed, after which it remains in RAM from the time the computer is booted up to when it is shut down.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron Job failure - No such file or directory

Hi all, I'm having an issue with a script i wrote to pull information from the Amazon AWS API. Basically the script takes arguments from the command line and attempts to grab user information for each AWS access group. The command is issued like this: # sh <outputFileName>... (3 Replies)
Discussion started by: ChocoTaco
3 Replies

2. UNIX for Advanced & Expert Users

[Tip] How to add individual delays to a cron job?

ofIn a big Unix environment you likely install cron jobs like this on a thousand systems: 39 15 * * * { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1If all the system clocks are synchronized (usually via NTP), these jobs run *exactly* at the same time. If the cron job accesses a shared... (2 Replies)
Discussion started by: MadeInGermany
2 Replies

3. UNIX for Advanced & Expert Users

[Tip] How to add an application cron job?

A well established form of application cron jobs look like this: 39 15 * * * && /usr/local/monitoring/oracle/ >/dev/null 2>&1The repetition makes it a long line, hard to read, hard to maintain. I suggest the following instead: 39 15 * * * { /usr/local/monitoring/oracle/ (1 Reply)
Discussion started by: MadeInGermany
1 Replies

4. Shell Programming and Scripting

How to write cron job for calling sql function database is postgres

Hi, Please help me to write cron job for calling sql function daily. I have Postgres database. (1 Reply)
Discussion started by: kulbhushan
1 Replies

5. Shell Programming and Scripting

How to add cron job in /etc/crontab

Hi, How to add a cron job in /etc/crontab using a shell script.??:confused: Actually the requirement is we need to run a script say, every 10 min through “cron”. That can be achieved by adding the below code line in the /etc/crontab , (i.e., “crontab -e ” command to add this to the... (4 Replies)
Discussion started by: Dedeepthi
4 Replies

6. Shell Programming and Scripting

Running script file using cron job every 5 second

Hi All, I beginner in unix, i have no idea how to set the script file using cron job every 5 second. I also want to execute automatically the output to text file.This is my script name and my textfile abc.txt. (6 Replies)
Discussion started by: mastercar
6 Replies

7. Shell Programming and Scripting

Cron job to move file from one directory to another

Hi, I have following directory structure Media (Inside media directory I have two folders namely videos and images) -->videos -->images Inside media directory I have some video files with extension .mp4 and images with extension of .jpg and .jpeg I want to write a cron job which will... (3 Replies)
Discussion started by: tusharkale
3 Replies

8. Shell Programming and Scripting

file size-cron job

Dear all, I have the following case,, i need to transfer a group of file from one server to another ....when the size of any of these file reach a specified value (Ex: 10MB) i need to transfer it to another server problem is that i dont know how to determine when the size of the file... (1 Reply)
Discussion started by: mm00123
1 Replies

9. UNIX for Dummies Questions & Answers

cron job to run php file

Hello, I have searched and searched google to do this and i want my websever to be able to run a php file everyday automatically. How do I go about doing this? Php is installed as an apache module not CGI. Thank you! (3 Replies)
Discussion started by: christo16
3 Replies

10. Shell Programming and Scripting

Conditional File Movement script scheduled using CRON job

Hi All, i am trying to automate a process and have to create a unix script like wise. I have a scenario in which i need to automate a file movement. Below are the steps i need to automate. 1. Check whether a file (Not Fixed name-Pattern search of file say 'E*.dat') is present in a... (2 Replies)
Discussion started by: imu
2 Replies

Featured Tech Videos