3

Hi I am trying to use screen as part of a cronjob.

Currently I have the following command:

screen -fa -d -m -S mapper /home/user/cron

Is there anyway I can make this command do nothing if the screen mapper already exists? The mapper is set on a half an hour cronjob, but sometimes the mapping takes more than half an hour to complete and so they and up overlapping, slowing each other down and sometimes even causing the next one to be slow and so I end up with lots of mapper screens running.

Thanks for your time,

2
  • 2
    This is a straight-forward question about how to program a shell script for use with cron. As such, it is on-topic for StackOverflow. Commented Jul 10, 2011 at 15:41
  • 1
    @Jonathan: I agree, I don't understand the close votes. Commented Jul 10, 2011 at 15:43

2 Answers 2

6
ls /var/run/screen/S-"$USER"/*.mapper >/dev/null 2>&1 || screen -S mapper ...

This will check if any screen sessions named mapper exist for the current user, and only if none do, will launch the new one.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer, and very clever solution! I have bookmarked this for future use!
2

Why would you want a job run by cron, which (by definition) does not have a terminal attached to it, to do anything with the screen? According to Wikipedia, 'GNU Screen is a software application which can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions'.

However, assuming there is some reason for doing it, then you probably need to create a lock file which the process checks before proceeding. At this point, you need to run a shell script from the cron entry (which is usually a good technique anyway), and the shell script can check whether the previous run of the task has completed, exiting if not. If the previous incarnation is complete, then the current incarnation creates a lock file containing its PID and runs the job. When it completes, it removes the lock file. You should review the shell trap command, and make sure that the lock file is removed if the shell script exits as a result of a trappable signal (you can't trap KILL and some process-control signals).

Judging from another answer, the screen program already creates lock files; you may not have to do anything special to create them - but will need to detect whether they exist. Also check the GNU manual for screen.

3 Comments

Actually there are several reasons. One would be you want to be able to come along later and check out how the job is progressing and manually intervene on an automatically started job. The other would be if you want cron to add or modify something already running in screen.
For example running game servers, for the reasons mentioned above.
@Caleb: if you want to check how a job is progressing by its text output, a more typical thing to do might be to redirect the cronjob's output to a file and just read it when you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.