Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can somebody show me how to make a program to do this action: after 5 minutes

echo "80" > /sys/class/leds/blue/brightness

I want this program run in the background (like rngd service) I can't do this because I don't know so much about Linux.

share|improve this question

4 Answers 4

up vote 13 down vote accepted
( sleep 300 ; echo "80" > /sys/class/leds/blue/brightness ) &

That way your script continues, or you restore control immediately, while a new background task of the script starts, with two commands: sleep, and echo.

The common error is trying to give either sleep or echo or both the & which will not work as intended. Launching a series of commands in () though spawns them in a separate shell process, which you can then send whole into background with &.

share|improve this answer
    
Yes I agree, but that can also be part of the user reflection ;-) –  Kiwy Nov 28 '13 at 15:01
    
Other answers seem to miss the "in background" part. No, I was pretty explicit about that :/ –  goldilocks Nov 28 '13 at 15:02
    
@goldilocks: You added the sending into background part only after I posted my answer. –  SF. Nov 28 '13 at 15:25
    
No, my original answer was to run a function containing sleep with & to background the sleep; "you need to background the sleep as well" was in the first line from the beginning. If you don't believe me, look at the edit history. And my answer is 5 minutes older than yours :P –  goldilocks Nov 28 '13 at 15:42
1  
@gekannt: ...uh, the question is "How to run a command in the background with a delay?" - how would you achieve it otherwise? (and no, if you don't background the () group, the original shell will pause until the spawned one finishes.) –  SF. Sep 5 '14 at 11:58

use the at command

echo "echo \"80\" > /sys/class/leds/blue/brightness" | at now + 5 min

that will run in the background

share|improve this answer
2  
This is the right way to do it, but it does need the atd daemon running and on most systems will need to have a package installed. –  Julian Nov 28 '13 at 23:17
    
You can use single quotes to avoid the escaping, and forego echo altogether by using a heredoc or herestring. –  Chris Down Dec 2 '13 at 15:49

If you want something to run in 5 minutes, but the rest of your program to continue (or finish), you need to background the sleep as well:

#!/bin/bash

runWithDelay () {
    sleep $1;
    shift;
    "${@}";
}

runWithDelay 3 echo world &
echo hello    

This will print hello and then 3 seconds later (after the main program has exited), print world.

The important part is the & to fork the function execution into the background.

share|improve this answer
1  
May I suggest runWithDelay () { sleep $1; "${@:2}"; } instead? –  manatwork Nov 28 '13 at 15:00
1  
I think it should read doWhateverWithDelay 3 & in your second example. –  Baarn Nov 28 '13 at 15:01
    
@manatwork : Cheers. –  goldilocks Nov 28 '13 at 15:06
1  
@goldilocks, the essential in using $@ is that you can quote it and will be expanded as list of quoted words. So better make it "$@" (or "${@}" if you like the braces). See pastebin.com/MU7hbB2C –  manatwork Nov 28 '13 at 15:15
    
@manatwork : Point taken ;) I did try that using args with echo (-n), but obviously that was a bit naive. –  goldilocks Nov 28 '13 at 20:32

A simple way is using crobjob. You can configure cronjob running every 5 minutes by using this command:

$ crontab -e

Then add the following line: 5 * * * * echo "80" > /sys/class/leds/blue/brightness

share|improve this answer
3  
That is not “every 5 minutes” but every hour at minute 5. –  manatwork Nov 28 '13 at 15:53
1  
you meant */5 * * * .... That's every 5 minutes. –  slm Nov 29 '13 at 3:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.