Score:1

kill app in a given timeframe, bash script

us flag

I'm new at bash and I am trying to make this, here is a pseudo code:

id= "pidof rhytmbox" (rhytmbox changes pid everytime i start it)

echo "Input time: "
read time

sleep $time
kill $pid

can you assist me?

Score:1
us flag
pid=$(pidof rhythmbox)
echo $pid

echo "Enter time: "
read time
sleep $time
kill $pid

This one worked!

Score:0
om flag

I'll provide some hints.

Bash has some ways to assign the output of a command to a variable.

pid=$(pidof top)
pid=`pidof top` 

For the rest you're basically on the right track. You should probably validate that the user is in fact entering a integer, and not some other text such as half an hour.

John Boro avatar
us flag
thank you --- pid=$(pidof rhythmbox) echo $pid echo "Enter time: " read time sleep $time kill $pid
vidarlo avatar
om flag
If this answered your question, please [accept it](https://askubuntu.com/help/someone-answers) by clicking the checkmark on the left. If you get new and better answers, you can change your accepted answer at any time.
Score:0
cn flag

If you intend to run your command with a time limit you can make use of timeout:

#!/bin/bash

read -r -p \
    "Input time in minutes: " duration

[[ ! $duration =~ [^0-9] ]] && \
    timeout ${duration}m setsid rhytmbox &>/dev/null &

read has a prompt option which is shown before attempting to read an input. You can use a regex statement to ensure user input only contain digits. Now, I usually want to put long-running application in the background in case I close the terminal, you can accomplish this with setsid command &

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.