How to create symlink for latest file only?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create symlink for latest file only?
# 8  
Old 04-17-2019
Yes, and ensure a successful cd:
Code:
cd /var/log && ln -s "$(ls -r *error_log.* | head -1)" error_log

BTW ls -r is good here; ls -t would really sort by mtime.
# 9  
Old 04-18-2019
Quote:
Originally Posted by MadeInGermany
Yes, and ensure a successful cd:
Code:
cd /var/log && ln -s "$(ls -r *error_log.* | head -1)" error_log

BTW ls -r is good here; ls -t would really sort by mtime.
I gave below command in crontab, it executes, but failed with rc-2
Code:
13 16 * * * cd /var/log/apache && ln -s $(ls -r *error_log.* | head -1) error_log
14 16 * * * unlink /var/log/apache/error_log

From /var/cron/log -->

>  CMD: cd /var/log/apache && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 16954 c Thu Apr 18 16:09:00 2019
<  root 16954 c Thu Apr 18 16:09:00 2019 rc=2


Last edited by solaris_1977; 04-18-2019 at 08:15 PM..
# 10  
Old 04-19-2019
That is not very surprising. You try to create a symlink at 4:13 pm (which fails because a symlink by that name already exists) and then you remove the symlink at 4:14 pm.

Furthermore, due to occasionally heavy load conditions, there is no guarantee that one cron job scheduled to start at a given time will complete before another job scheduled to run one minute later.

Why not do both in one step (assuming that you aren't really trying to create a symbolic link that has a one minute lifetime):
Code:
13 16 * * * cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log


Last edited by Don Cragun; 04-19-2019 at 03:47 AM.. Reason: Fix typo: s/lifetime:/lifetime):/
These 2 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 04-19-2019
I'm a bit surprised that both the log entries are at 16:09h when the crontab entries are for 16:13h and 16:14h? Sure they belong together?
# 12  
Old 04-19-2019
May be I missed to copy correct content. Here I tried it again. I made two attemps. One job created for 23:58 and another time at 00:01
Code:
-bash-3.2# crontab -l | tail -2
58 23 * * * cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
01 00 * * * cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
-bash-3.2#
-bash-3.2# tail -f  /var/cron/log
<  root 28826 c Thu Apr 18 16:13:00 2019 rc=2
>  CMD: unlink /var/log/apache/error_log
>  root 29897 c Thu Apr 18 16:14:00 2019
<  root 29897 c Thu Apr 18 16:14:00 2019 rc=255
>  CMD:  cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 26545 c Thu Apr 18 23:58:00 2019
<  root 26545 c Thu Apr 18 23:58:00 2019 rc=2
>  CMD: cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 3093 c Fri Apr 19 00:01:00 2019
<  root 3093 c Fri Apr 19 00:01:00 2019 rc=2
^C
-bash-3.2# ls -l /var/log/apache/error_log
/var/log/apache/error_log: No such file or directory
-bash-3.2#

But if I run command manually, it is able to create
Code:
-bash-3.2# cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
-bash-3.2# ls -l /var/log/apache/error_log
lrwxrwxrwx   1 root     root          18 Apr 19 00:03 /var/log/apache/error_log -> error_log.20190419
-bash-3.2# rm -f error_log
-bash-3.2#

To test, if cron is working with root, I made another entry of date command
Code:
32 00 * * * date>/tmp/date.out

-bash-3.2# tail -f  /var/cron/log
<  root 20623 c Fri Apr 19 00:20:22 2019
>  CMD: date > /tmp/date.out
>  root 10651 c Fri Apr 19 00:26:00 2019
<  root 10651 c Fri Apr 19 00:26:00 2019
>  CMD: cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 12318 c Fri Apr 19 00:27:00 2019
<  root 12318 c Fri Apr 19 00:27:00 2019 rc=2
>  CMD: cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 13871 c Fri Apr 19 00:28:00 2019
<  root 13871 c Fri Apr 19 00:28:00 2019 rc=2
>  CMD: cd /var/log/apache && rm -f error_log && ln -s $(ls -r *error_log.* | head -1) error_log
>  root 23263 c Fri Apr 19 00:31:00 2019
<  root 23263 c Fri Apr 19 00:31:00 2019 rc=2
>  CMD: date>/tmp/date.out
>  root 25056 c Fri Apr 19 00:32:00 2019
<  root 25056 c Fri Apr 19 00:32:00 2019
^C
You have new mail in /var/mail/root
-bash-3.2# ls -l /tmp/date.out
-rw-r--r--   1 root     root          29 Apr 19 00:32 /tmp/date.out
-bash-3.2#

Not sure if "rc=2" is giving some indication

Last edited by solaris_1977; 04-19-2019 at 05:49 AM..
# 13  
Old 04-19-2019
It certainly does and might be listed in the Solaris documentation, to which I have no access. cron jobs send their output - if any - by mail. You seem to have received mail(s). What do they say? You might want to add some debug info to your commands - intersperse some echoes.
# 14  
Old 04-19-2019
rc=2 means exit status 2.
Quote:
You have new mail in /var/mail/root
Look at root's mail with
Quote:
tail -200 /var/mail/root
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Symlink

I know how to create a symlink but I am getting an error message that the file already exists. Also, my symlink doesn't point from target directory to the path correctly, Here is an example of the path to my folder structure path: cd /wkspce/wff/DEV/jobs/DEL the folder structure is: ... (3 Replies)
Discussion started by: dellanicholson
3 Replies

2. Shell Programming and Scripting

Symlink creation

I am trying to setup my plex server to use symlinks rather than host the movie files. in my storage directory, i have both movies(some in subdirectory of the name and some just in the parent directory) and tvshows, which have subdirectories for each season, which contains the episodes i would... (3 Replies)
Discussion started by: bandion
3 Replies

3. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

4. UNIX and Linux Applications

Tomcat 6.0 fails to read symlink(symbolic link) file

Hello all experts, Im in a situation where Tomcat simply does not want to read this file through the symlink.... I checked permissions..OK Also checked file & tomcat owner...all OK. This is what I have my /tomcat/conf/Catalina/local/appname.xml <Context> <Resource name="jdbc/black" ... (3 Replies)
Discussion started by: KingaKoopa
3 Replies

5. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

6. Shell Programming and Scripting

create t a filelist with the latest file by YYYYMMDD and move to subfolder

Hi all, I am receiving files named like: ABC_20120730.csv ABC_20120801.csv ABC_20120812.csv They are residing in a folder named Incoming. I am supposed to write a script that will create a filelist which will contain only the name of the latest file beginning with ABC_, by YYYYMMDD... (3 Replies)
Discussion started by: kedrick
3 Replies

7. Shell Programming and Scripting

File count for symlink using find command

Hi Guys, The script which I am using works really good for finding the file count for files that are not symlink. I know I can use find command like: find . -type l | wc -l This way I can get filecount of the symlink but is there a one liner to use -type l and -type f option ? That is... (4 Replies)
Discussion started by: dixits
4 Replies

8. Shell Programming and Scripting

ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist

Hi Forums, I got a little problem, I made a few modifications to the code of the launch script of a testing server(minecraft) and now updating is broken aswell as the automatic directory creation. These Lines somehow create an endless symlink that refers to itself and I don't know how to fix... (0 Replies)
Discussion started by: Xaymar
0 Replies

9. Solaris

symlink on link file

Hi all, I want to create a symlink on a link file, i mean, there is a file which is actually a symlink of some version. Now i want to create one more symlink on that link file. EX: there is a file: uat -> version prod -> version Now i want to create one more link on these 'uat' and... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

10. UNIX for Dummies Questions & Answers

creating symlink

hi... I have a folder<abc> under /root folder. I want to create a symlink such that when i click on folder<abc> under root, should display my home folder (home/krish). Immediate inputs appreciated..... (1 Reply)
Discussion started by: rama.honge
1 Replies
Login or Register to Ask a Question