LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2020, 10:52 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
Bash Script: How to remove temp file when terminated from keyboard?


Hello All

I have written a bash script that encodes all video files in a directory to h265 (hevc) codec to reduce storage file size. First, the script encodes the video file to a temp file, and then when encoding is successful, it renames that temp file to the original file and finally removes that temp file.

But if I press Ctrl+C in-between, I want to remove that temp file. I have specified trap command at the beginning of the script. The temp file is created later.

How do achieve that? Is it even possible?

For some reason, I'm unable to copy and paste the script, so I've attached full script for reference.

Also, this is my first big script. I've concentrated only on logic rather than performance. I really appreciate pointing to any flaws in it.

Thanks.
Attached Files
File Type: txt hevc.txt (6.1 KB, 28 views)
 
Old 05-12-2020, 10:57 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you need to set a trap on EXIT
 
1 members found this post helpful.
Old 05-12-2020, 11:59 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Firstly, I'd use mktemp for this
Code:
outputFileName="$inputFileFldr/$(tr -c -d 0-9 < /dev/urandom | head -c 15).mp4"
Code:
outputFileName=$(mktemp -u -p "$inputFileFldr" XXXXXXXXXXXXXXX.mp4)
Secondly, as pan64 said, you should remove this file in the trap action on EXIT:
Code:
trap 'rm -f "$outputFileName" >/dev/null 2>&1' 0
Thirdly, you should trap INT, and maybe also HUP, QUIT, PIPE, TERM, and ensure exit on them that will lead to the file being deleted as well.
Code:
trap 'echo "Oops! hevc ($$) is terminated."; exit 3' 1 2 3 13 15
 
1 members found this post helpful.
Old 05-12-2020, 12:03 PM   #4
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by pan64 View Post
you need to set a trap on EXIT
Do you mean at the end of the script?

Trap code:
Code:
trap ' echo "Oops! hevc ($$) is terminated." ; exit 3 ' INT
When put at the beginning of the script:
Code:
$ hevc -e
Converting: 1/1
    Input : 'Spencer.mkv'
    Output: './355932967073595.mp4'
    Info  : '11.7 MiB' 'AVC' '1920x1080' '00:00:12.041' '160090'
frame=   29 fps=7.6 q=-0.0 Lsize=     235kB time=00:00:01.31 bitrate=1460.6kbits/s speed=0.344x    
Oops! hevc (32461) is terminated.
But when put at the end of the script:
Code:
$ hevc -e
Converting: 1/1
    Input : 'Spencer.mkv'
    Output: './095847693918920.mp4'
    Info  : '11.7 MiB' 'AVC' '1920x1080' '00:00:12.041' '160090'
frame=   28 fps=7.4 q=-0.0 Lsize=     228kB time=00:00:01.25 bitrate=1488.8kbits/s speed=0.333x    
COMMAND failed
 
Old 05-12-2020, 12:07 PM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by shruggy View Post
Firstly, I'd use mktemp for this
Code:
outputFileName="$inputFileFldr/$(tr -c -d 0-9 < /dev/urandom | head -c 15).mp4"
Code:
outputFileName=$(mktemp -u -p "$inputFileFldr" XXXXXXXXXXXXXXX.mp4)
Secondly, as pan64 said, you should remove this file in the trap action on EXIT:
Code:
trap 'rm -f "$outputFileName" >/dev/null 2>&1' 0
Thirdly, you should trap INT, and maybe also HUP, QUIT, PIPE, TERM, and ensure exit on them that will lead to the file being deleted as well.
Code:
trap 'echo "Oops! hevc ($$) is terminated."; exit 3' 1 2 3 13 15
Need some time to digest. I'll get back after understanding traps more. Thanks
 
Old 05-12-2020, 12:18 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
no, there is a special trap:
Code:
trap cleanup EXIT
function cleanup() {
   cleanup at exit
}
 
1 members found this post helpful.
Old 05-12-2020, 10:03 PM   #7
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
This is what I have in some of my scripts as per pan64 above:-

trap CleanUp 1 2 3 15 18

CleanUp()
{
rm -f $tempfile
exit 0
}

where "tempfile" is defined somewhere in your script.
 
1 members found this post helpful.
Old 05-13-2020, 10:10 AM   #8
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
I ended up doing the following changes.

Code:
#!/bin/bash

CleanUpTrap() {
        echo "Oops! hevc ($$) is terminated."
        find "$PWD" -type f -name "hevctmp420_*.mp4" -delete 2>/dev/null
        exit 2
}
trap 'CleanUpTrap' SIGHUP SIGINT SIGQUIT SIGQUIT SIGTERM
.
.
.
outputFileName="$(mktemp -u -p "$inputFileFldr" hevctmp420_XXXXXXXXXXXXXXX.mp4)"
.
.
.
I didn't specify the EXIT signal because the regular and clean exit of the script itself takes care of the temp file. The problem was only when it was interrupted forcefully.
 
Old 05-13-2020, 10:19 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
the exit trap executed always, regardless of any error/signal/whatever. But you know what you need.
 
  


Reply

Tags
shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 12:30 PM
[SOLVED] init.d script - /sbin/service: line 66: 23002 Terminated wazzgod Linux - Software 2 10-29-2011 10:06 AM
Shell script terminated after ftp transfer jmsans Programming 7 12-29-2009 08:10 AM
lm_sensors cpu temp v.slightly different to MOBO temp on Intel D865GLC sc_3007 Linux - Hardware 5 11-13-2009 12:17 PM
Script terminated in terminal window 786 Linux - General 4 11-22-2008 05:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration