简体   繁体   English

启动ruby脚本onLoad ubuntu

[英]start ruby script onLoad ubuntu

i want create simple application for notification in ubuntu... For example: in 18:00, 21:00, 24:00 i want to receive message from ubuntu 我想在ubuntu中创建用于通知的简单应用程序...例如:在18:00、21:00、24:00我想从ubuntu接收消息

simple method for message is: 消息的简单方法是:

def alarm
  %x{notify-send -i rhythmbox Test "Hello world" }
end

alarm

and when i sart this file ( like: ruby somefilename.rb ) 当我出售此文件时(例如:ruby somefilename.rb)

i got notification message. 我收到通知消息。

Question: how to cause this file run in 18:00, 21:00, 24:00

PS I know that there are many similar programs (for the experience like to know how to do that) 附言:我知道有很多类似的程序(对于经验丰富的人,他们想知道该怎么做)

UD Using crontab: UD使用crontab:

* * * * * %x{notify-send -i rhythmbox Test "Hello world" }

Every minutes should start this script - but nothing happend 每分钟都应该启动此脚本-但什么都没发生

in some examples i saw that instead of script we put path to them: something like this: 在某些示例中,我看到了我们使用脚本而不是脚本:它们是这样的:

* * * * * ruby /home/Home/myDisk/somefile.rb

how open with ruby this file or launch notify-send script in terminal without ruby ? 如何用ruby打开该文件或在没有ruby的终端中启动notify-send脚本?

Good tools are cron and anacron . cronanacron是很好的工具。

To do it in Ruby, one way is to use the Ruby sleep method: 要在Ruby中进行操作,一种方法是使用Ruby sleep方法:

seconds = 18 * 60  # 18 minutes * 60 seconds/minute
sleep(seconds)
alarm

To use your "wake up" times in a loop: 要循环使用“唤醒”时间:

start = Time.now
for wake in [18, 21, 24]
  seconds = wake * 60 + start - Time.now
  sleep(seconds)
  alarm
end

Also see Ruby Thread to do alarms in parallel: 另请参见Ruby Thread并行执行警报:

for wake in [18, 21, 24]
  threads << Thread.new(wake) {|wake|
    sleep(wake * 60)
    alarm    
  }
end
threads.each {|t| t.join }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM