1

I have a script (moves log file in backup dir) located in a shared folder that runs in ServerA. It is working as expected. I am planning to run this script in several servers, ie ServerB, ServerC, ServerD. However, since the script is located in a shared folder, it may cause issue if all servers will run this script simultaneously. Can you please advise? I am thinking of putting a flag that checks if the script is being used. I am not yet sure on how.

Script

TIMESTAMP=`date "+%Y.%m.%d-%H.%M"`
LOGS="$(cat Apps.txt)"
LOGSUFFIX=".log"
APPLOGS_DIR="/APPSDIR"
BACKUP_DIR="/BACKUPDIR"

for LOG in ${LOGS} do if [ -f "${APPLOGS_DIR}/${LOG}${LOGSUFFIX}" ] then gzip "${APPLOGS_DIR}/${LOG}${LOGSUFFIX}" mv "${APPLOGS_DIR}/${LOG}${LOGSUFFIX}.gz" "${BACKUP_DIR}/${LOG}_${TIMESTAMP}.gz" echo "$Logs moved to backup directory " else echo "No logs" fi done

  • "it may cause issue if all servers will run this script simultaneously" ... why? There are no resources used which are specific to a single server. – Panki Oct 13 '20 at 09:21
  • @Panki, thank you for response. Will it not cause issue if ServerA is still using the script and then ServerB will use the same script? Cron will execute the script in several servers at the same time. – UnixDummy001 Oct 13 '20 at 09:24
  • You can run multiple instances of the same script. But you have to ensure that the actions taken by each instance does not conflict with another instance. In this case, you're working with the /APPSDIR & /BACKUPDIR paths. Are these paths unique to each server, or are they shared between servers? – Haxiel Oct 13 '20 at 11:19
  • @Haxiel thank you for your response. /BACKUPDIR is a shared location between servers. That is why I am thinking of some checks/flag before running the script. Like, it will not run if this script is being used is a server. – UnixDummy001 Oct 13 '20 at 12:43
  • @UnixDummy001 Okay, if /BACKUPDIR is a shared path, the main concern would be to ensure that the log files from different servers go to different locations. You wouldn't want a log file from one server to overwrite the log file from another server. I can see that you have a timestamp in the name, but it's limited to minutes, which still leaves a fair possibility of collision. Once this resolved, you shouldn't have any conflicts for the script instances. (1/2) – Haxiel Oct 13 '20 at 13:38
  • @UnixDummy001 If you still want to keep the script instances separate (i.e. not executing at the same time), the usual approach is to use a lock file. See this QA for a lot more details. (2/2) – Haxiel Oct 13 '20 at 13:40
  • "Running a script" only means that an instance of bash running on Server{A B,C,D} will READ the script. No conflict there. Deconflicting simultaneous runs can involve lock files (man -k lock), or use of $(hostname) in filenames or directory names. – waltinator Oct 13 '20 at 16:17
  • @Haxiel the logfiles that will be moved to the backup directory will have different filesname. For sample ServerA has LogA, ServerB has LogB. This should not be a problem? – UnixDummy001 Oct 14 '20 at 05:43
  • @waltinator thank you for your response. There will be no problem with the filenames since Server{A,B,C,D} has different logfiles. This should not be a problem? – UnixDummy001 Oct 14 '20 at 05:46

0 Answers0