Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have six shell scripts I start when I log in. The shell scripts kick off Python scripts which go out and scrape various things from the net like news headlines, weather info etc., which I add to my desktop via conky.

Problem is starting all of these shell scripts each time is a real pain.

I have looked for and tried some things but nothing seems to work.

Can someone here shed some light on the best way to do this pls. By the way I can't combine the shell scripts in a single file because they all 'sleep' for a different duration after the initial command is applied.

Thanks.

-- Edit (adding the following comments) --

All of the scripts have the following format:

#!/bin/bash

while true; do
    python /home/scott/debkaget.py > /home/scott/debka.txt
    sleep 120
done

Could that be added to the profile? I need the sleep aspect to it. I am running KDE 4.9.

share|improve this question

migrated from stackoverflow.com Aug 25 '12 at 11:00

This question came from our site for professional and enthusiast programmers.

1  
Do you use a window manager? Which one? –  choroba Aug 25 '12 at 0:59
    
What is the 'pain'? –  New Alexandria Aug 25 '12 at 1:34
    
Well the pain is having to open up a terminal, type 'runshell1.sh', ctrl-t, and do this again and again. And my shell script list keeps growing, 7 now. :( –  Scott Richardson Aug 25 '12 at 1:52
    
I think you should take the 2 parts of my answer and apply it in the locations NormanRamsey indicates. –  New Alexandria Aug 25 '12 at 16:32
    
I was curious, too, for an explicit example - so I decided to build one. I'll probably use this myself next time. –  New Alexandria Aug 25 '12 at 17:28
show 1 more comment

3 Answers

If you're "adding to your desktop" you have a window manager running, and if you are using bash it's probably Linux. In that case you're probably best off launching your scripts into the background from an Xsession file, either ~/.Xsession or /etc/X11/Xsession or a variant. For more info try

man 5 Xsession
man 1 xinit
share|improve this answer
    
While I and Leffler presented general purpose answers, this answer has the most domain-specific information. –  New Alexandria Aug 25 '12 at 16:30
add comment

One possibility is to add the appropriate invocations to your .bash_profile or .profile file. When you run them manually, do you add an & at the end to run them in background? If so, do that in your profile too.

The main issue here is 'what happens if you've already run a login shell'. Do the 6 scripts behave sanely if there's another copy already running? Do they bother to detect that there's another copy already running.

If you're logging in at a GUI desktop, then there are ways to run programs when the desktop starts; use it to run the scripts that you're trying to deal with. For more accurate instructions (from other people), provide more information about your environment.

share|improve this answer
    
Hi, thanks for the reply. Well to start I am running KDE 4.9, recently moved to this from Ubuntu 12.04. I was doing the same thing on Ubuntu though. –  Scott Richardson Aug 25 '12 at 1:48
    
Look to the Xsession suggestion by Mr Ramsey. –  Jonathan Leffler Aug 25 '12 at 1:56
add comment

You can use this basic strategy:

  • have scripts start by creating pid files as script_name.pid. Make the scripts abort if for their pid file exists
  • have scripts run in the background

When you invoke from your .bash_profile (or similar) then the pid files will prevent .bashrc from winding up more processes for each new shell.

Per NormanRamsey, you will add to the XSession (or other window manager):

# Web Scrapers
runshell_yahoonews.sh
runshell_blog_disney.sh
runshell_stackoverflow_election.sh
runshell_twitter_dad.sh
runshell_twitter_origamistork.sh
runshell_blog_daringfireball.sh

Consider writing bash functions. You could have a file lib_scrapers.sh

#!/bin/bash 
pid_path = "~/.X-scrapers/"
suff = '.pid'

function pidfile_create(invoker) {
  if [ ! -e $pid_path$invoker$suff ];
  then
    # make it
    echo $$ > $invoker$suff
  else
    # alert it existance
    cat $pid_path$invoker$suff
  fi
}
function pidfile_destroy(invoker) {
  rm $pid_path$invoker$suff
  if [ -e $pid_path$invoker$suff ];
  then
    echo "$pid_path$invoker$suff could not be destroyed properly"
  fi
}

then on line 2 of each of your scrapers (e.g. runshell_yahoonews.sh). This will prevent the script for executing if a scraper is already running.

lib_scrapers.sh

if [ pidfile_create($0) ]
then
  exit
fi

at the end of each scraper, put

pidfile_destroy($0)

You could have other tradeoffs, but this sounds reasonable for what you describe.

share|improve this answer
add comment

Your Answer

 
discard

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