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

5 Answers 5

up vote 11 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 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

I think I want to I disagree with most of the approaches mentioned here, even if they're mostly technically correct. It just strikes me as interesting that you don't mention what your point for this is? I guess what I mean is, why are you sending the signal via sysfs in the first place?...

Out of curiosity I googled the sysfs path you reference and came up with a lot of android related results. As luck would have it I happen to be typing this on my own android device so I opened a terminal and checked some stuff out...

$ cat /sys/class/leds/white/trigger
[none] battery-charging-or-full battery-charging\ 
battery-full battery-charging-blink-full-solid ac-online\
usb-online wireless-online mmc0 heartbeat rfkill1

Interesting... Back to Google again and I find kernel.org/doc/.../leds-class.txt...

...The class also introduces the optional concept of an LED trigger. A trigger is a kernel based source of led events. Triggers can either be simple or complex. A simple trigger isn't configurable (rats) and is designed to slot into existing subsystems with minimal additional code. Examples are the ide-disk, nand-disk and sharpsl-charge triggers. With led triggers disabled, the code optimises away.

Complex triggers whilst available to all LEDs have LED specific parameters (better...) and work on a per LED basis. The timer trigger (what's this?) is an example. The timer trigger will periodically change the LED brightness between LED_OFF and the current brightness setting. The "on" and "off" time can be specified via /sys/class/leds//delay_{on,off} in milliseconds. You can change the brightness value of a LED independently of the timer trigger. However, if you set the brightness value to LED_OFF it will also disable the timer trigger.

You can change triggers in a similar manner to the way an IO scheduler is chosen (via /sys/class/leds/{device}/trigger) (yay). Trigger specific parameters can appear in /sys/class/leds/ once a given trigger is selected. ...

I'm going to have a few pokes around at sysfs to see if i can figure out specifically how to do this and get back with you, but first I'll mention what tipped me off...

Generally speaking, you shouldn't be echoing much at all to sysfs in shell script if it can be helped, and, generally speaking, it can be. At least I found the idea of a scripted batch job a little strange.

It's true, it used to be the case that these types of things were more common, but since the advent of udev and parallel processed device handling, those types of approaches have quickly fallen out of favor, and for good reason. Almost every time I've found myself considering a shell-scripted sysfs poke or two I've also found reason to regret it. On the desktop, definitely, udev rules all the way, android...? Maybe I'm wrong. I'm about to find out.

But for the rest, for my money anyway, SF is about as right on as he could be given he the information he was provided. To add though, I should say you should really be doing job control with, you know, jobs. And trap, wait, kill, fg, bg, and the rest (that's actually close to all of them, not mentioning at and batch and that crew which have already been suggested, and which would be more to your purpose...).

But those things don't really cut it either. What you ask for is either a one-time call 5 minutes after completing a task, in which case you should really be sending your process a kill signal caught via trap, or, as I think, you want to implement a system service daemon, which will blink the LED every 5 minutes, which you should definitely be doing via trapped signals that are specified at boot by either an init script (which I hope for your system's security's sake doesn't just fork out sleep calls willy-nilly) isn't it?

If you're looking to implement a daemon then you'll want an /etc/{daemon}.conf in which you can define these kinds of things and to ensure that you fork as little as possible. The best way to do this is not to do so at all, and instead simply use another one of the many daemons that are surely already running on your system to do it for you, or to implement a simple init script that runs once and exits but configures things before it goes away like the kernel driver implemented timer described above. And so I'm off to look into that further...

share|improve this answer

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.