Touch - changing date and time


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Touch - changing date and time
# 8  
Old 01-09-2015
Quote:
Originally Posted by brjohnsmith
in your script, the user1 is a static user, as the file could be created by anyone, how do I change it to be according to the file owner?
Who do you want to be able to do this? Not just anyone should be able to. There must be reasons this is restricted, it shouldn't be done in a way that allows anyone to do so. Would making a group of users who're allowed to use this tool be okay?

Last edited by Corona688; 01-09-2015 at 03:50 PM..
# 9  
Old 01-09-2015
Hi,

the application handles somethings and all users allowed to run the application should be able to change the date and time. So, when the file is created, the owner is one, but I need to update the date and time, according to some rules on the application, and the solution was to use the touch command.Note: everyone that is allowed to run the application, belongs to the same group.

user1 - group app1
user2 - group app1

The process:

user1 creates a file - xxx.txt, today - 20150109 10:10....

tomorrow, the user2 running the application, for example, he accesses the file xxx.txt by the application, so, for my control and my reason, I need to change the date and time. note: if the user1 tries to read the file xxx.txt, it works - the touch command, because the owner is the same.

today, the application issues the touch command with 2 parameters: the date/time and the folder+filename. (parameters: 201501100830 /xxx/yyy/zzz/xxx.txt), the final command is: touch -c -t 201501100830 /xxx/yyy/zzz/xxx.txt

If the solution is to run as root, I will need to do it. Of course, if there is other way to do it, and with safety, I would like to know,

One way to use your script, considering dynamic user, is the application read before the owner, and instead of use the touch command, it would be used your script with 3 parameters: the owner, the date/time and the folder+filename.

would it be a solution? on sudo statement in your script, would the password be requested? if so, it would be not the solution. I need to avoid any interactive session.

if I understood the idea of your script, the folder where the script will be stored, it will be always the same, but the folder and the filename where the date/time will be changed, not.

Please, let me know if it is clear and if there would be a solution for me.

tks.
# 10  
Old 01-09-2015
Quote:
Originally Posted by brjohnsmith
Note: everyone that is allowed to run the application, belongs to the same group.

user1 - group app1
user2 - group app1
Okay. That helps a lot. Just be sure to take a good look at what's inside the 'app1' group. You don't want people to be able to touch root's timestamps, etc. You may find a few users you you should specifically prevent them from running as.

Code:
# sudoers file

%app1 ALL=(%app1) NOPASSWD:  /path/to/update-time.sh

This would allow anyone in the app1 group to run /path/to/update-time.sh as any other user in the app1 group. Still no need to risk giving anyone root.

So:

Code:
#!/bin/sh

if [ "$#" -ne 2 ]
then
        echo "Usage:  $0 timestamp filename" >&2
        exit 1
fi

if ! [ -e "$2" ]
then
        echo "File $1 does not exist" >&2
        exit 1
fi

# Can also use 'stat' to get this on some systems
NEWUSER="$(ls -l "$1" | awk '{ print $3 }' )"

# If we are not the owner of the file, restart this script from the beginning
# as $NEWUSER via sudo.
[ "$USER" = "$NEWUSER" ] || exec sudo -u "$NEWUSER" "$0" "$@"

# Touch the file with an arbitrary timestamp.
touch -c -t "$1" "$2"


Last edited by Corona688; 01-09-2015 at 05:54 PM..
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 01-09-2015
Hi Corona688,

thank you very much for the code.

Quote:
# If we are not the owner of the file, restart this script from the beginning
# as $NEWUSER via sudo.
[ "$USER" = "$NEWUSER" ] || exec sudo -u "$NEWUSER" "$0" "$@"
I am not an expert on Linux and script, so, could you please explain better the statements above? I am not sure what I need to do when you say: restart this script from the beginning as $NEWUSER via sudo.

I thought that the script should executed at once, no matter the owner. Even I need to execute the script again how to execute the script as sudo, how would it be? Just remembering, the application will send to Linux the script execution, and the execution needs to be at once - there is no interactive session to execute the script, I am not able to send to Linux 2 commands, just 1.

what happen if I always consider that the user/owner is different and sudo is always executed? (even if I could have the owner as the user)

tks.
# 12  
Old 01-11-2015
Hi Corona688,

I have tested the code, and it worked, and I did not need to change/start when the user is not the owner. Perfect!!!. I just had to adjust part of the code below, because I assumed that the Parm 1 is the date and Parm 2 is the folder/file.


Quote:
NEWUSER="$(ls -l "$1" | awk '{ print $3 }' )"
TO:


Quote:
NEWUSER="$(ls -l "$2" | awk '{ print $3 }' )"
Thank you very much for the assistance on this issue.
# 13  
Old 01-12-2015
That exec line is a convenience thing so the user can run /path/to/script.sh instead of sudo -u username /path/to/script.sh. If the user is different, it runs sudo -u username /path/to/script for them. $0 is "/path/to/script.sh" so I don't have to hardcode what the script is named. "$@" is "all arguments, properly quoted". exec means "replace the current script with the command I'm running now" so all the lines below are ignored when the user is wrong.

Sorry about the bug, you've got it figured out.

Last edited by Corona688; 01-12-2015 at 02:40 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. SCO

Changing date time will cause the X11 restart

I am using SCO Unix System V/386 Release 3.2v4.2 in some legacy machine. I find that when I change the date time of the system, it will sometimes restart to the scologin page It seems to be related to the X11 because other sessions (not scologin session) will not be affected. Is there... (10 Replies)
Discussion started by: dannychan
10 Replies

2. UNIX for Advanced & Expert Users

Help with touch: bad time specification

Here is the part of the script: I have modified the file name.:) SSFILE=${My_HOME_DIR}/log/my_file_ss.log export MM=`date '+%m'` export DD=`date '+%d'` export HH=`date '+%H'` export MIN=`date '+%M'` export HOURAGO=`echo ${HH} -1 |bc ` echo $HOURAGO export TTIME=${MM}${DD}${HOURAGO}00... (5 Replies)
Discussion started by: N1a_Raider
5 Replies

3. Shell Programming and Scripting

How to change time stamp with touch command?

Hi, I wish to change time stamp of a directory with all its subdirectories and files on server. I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data: find * -type d -exec touch... (5 Replies)
Discussion started by: bioinfo
5 Replies

4. Linux

Strange, date and touch returning different times.

Hi guys, Hope one of you guys can sort this out for me. I first noticed this when users were uploading files from our website to our server and the time uploaded was +18 hours ahead of the current time. At first I assumed this was an issue with the coding, but didn't think it was likely as we... (2 Replies)
Discussion started by: markn86
2 Replies

5. UNIX for Advanced & Expert Users

Changing Time and date on Virtual server

Hello Everyone I am running a Sunos 5.10 Generic_138888-02 sun4u sparc SUNW,SPARC-Enterprise machine with more than one virtual servers are on this box. My question is possable to change the date and time on the virtual server without change the time on the main server? Thanks Peter (4 Replies)
Discussion started by: Peterh
4 Replies

6. UNIX for Dummies Questions & Answers

touch -t time, using different userid

Hi, I am reciveing files from a remote system on my linux box. These files are named based on time, which I can use to 'touch' the time . I can access/modify these files using my id. but when I tried touching time using my id I am getting error; touch -t 1001261234 1001261234_job2333... (15 Replies)
Discussion started by: rajivbravo
15 Replies

7. Shell Programming and Scripting

how to make a log.txt and add date and time when use ls,touch and find

Hey guy, how to make the log.txt file and record date and time when ls, touch and find command run? Thanks Boly (13 Replies)
Discussion started by: chenboly
13 Replies

8. Shell Programming and Scripting

how to touch a file with prev time stamp

i want to find the files which are modified in last 30 to 120 minutes i am using "find . -mmin +30 -mmin -120 " it is giving me the error find: bad option -mmin find: path-list predicate-list can somebody help me out . Thank you (5 Replies)
Discussion started by: Prat007
5 Replies

9. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies
Login or Register to Ask a Question