1

Ubuntu 16.04 LTS user

Hi guys, I am a newb here and I have been searching these forums and google to no avail. My searches have not found the specific info that I need.

This is personal project of mine to help me get accustomed to setting system jobs and scripts that will run daily/monthly/any time refrence.

I have already (inefficiently) created several folders for the 10-12 users on my Ubuntu machine already where I want this job to go. It is in their home directory in a folder titled 'ulogs'. That is the folder I will be hopefully have the various system wide jobs I create, save files for every user on the system.

I know this job is to be created in cron.daily as a script. I know how frequently I want to run it, perhaps twice a day at some point but will start with once a day right now.

In the script, what is the protocol to save the contents/end result of the job to the folder I've created and selected where I want to save a txt file of job?

For this job, I want to save the contents of the auth.log (the cron script is basically "cat /var/log/auth.log > /home/Users(1-12)/ulogs/daily-signin.txt")

What variables can I use instead of typing out every users username and folder path where the file should be saved.

I'll also want to append the date to the end of the file but I'll figure that out on my own, I can't have you guys doing all the work. I've been searching but I've bounced around from site to site and have not found an efficient way to save the file to every users folder path.

Please help!

1 Answers1

0

The following program will create a text file named daily-signin.txt in a directory named ulogs under all users home directory if you run this as root.

#!/bin/bash
path="/path/to/source/file";
for p in `cat "$path"`;
do
log=`cat /var/log/auth.log`;
mkdir -p /home/$p/ulogs/
echo "$log" >> /home/$p/ulogs/daily-signin.txt
done

Points to note

  1. The file /path/to/source/file contains the list of users in your machine. If any new user created and you want to get the log file in that user's home directory as well, you have to add the new username in the source.txt file.

  2. As this file is being created on another user's home directory, you will need to run this shell script with sudo privileges

  3. If you are running this script as a cronjob, you may follow this link in order to get this done.

Rooney
  • 965
  • I want to thank you for taking time to answer this. I will give this a go and let you know how everything turned out. Another side note, I have a user added to the 'root' group. When I tried to list a users crontab it said ' must be privileged to use -u' after running crontab -e -u . That account is part of the root group and sudo group. I figured it should have root capabilities. This is a side note, I'll go test out what you posted though. – Final Luminary Dec 03 '17 at 17:59
  • @FinalLuminary Have you tried it ? – Rooney Dec 04 '17 at 07:34