There is no straight forward answer to your question, other than "it depends on the environment" i.e. the GNU / Linux distribution.
Before I go into semi-relevant details, you should know that the reason why
sudo service import-image-service does not work is because the import command does not have enough information about the environment to know where and how to take the screenshot. Reading man sudo reveals:
sudo allows a permitted user to execute a command as the superuser or
another user, as specified by the security policy.
This is somewhat cryptic and learning about this "security policy" is fun, but perhaps not one of your favourite activities. I give you my word that this means commands started with sudo run in an isolated environment.
/etc/init.d/myservice works because environment-setup-policy takes steps to configure some sane defaults such as
export DISPLAY=:0.0. You will have to investigate the intricacies of the distribution in use to figure out how to properly inform a service about the presence of an X server. In the mean time, you can try
sudo -E service myservice
-E The -E (preserve environment) option indicates to the secu‐
rity policy that the user wishes to preserve their existing
environment variables. The security policy may return an
error if the -E option is specified and the user does not
have permission to preserve the environment.
warning: this section is semi-factual and biased historically
The services in a GNU / Linux system behave somewhat arbitrarily from one distribution to the next. Mostly, the difference is in the setup of the environment before the service is run. This depends on several factors, the most important ones being:
- init system
- plain System V init + posix shell (Slackware, Debian)
- OpenRC (Gentoo)
- Upstart (Ubuntu)
- systemd (many recent distros)
- distributions environment-setup-policy*
- details of the particular init script
Debian, Slackware and Gentoo (OpenRC) use a conceivably similar approach where /etc/init.d/ services are independent scripts that optionally get additional information from /etc/default/servicename, /etc/conf.d/servicename and similar. The scripts may or may not rely on distribution-specific init-function such as /lib/lsb/init-functions or /lib64/rc/sh/functions.sh. These additional shell libraries may get information (set up the environment) from additional distribution-specific sources.
Ubuntu (Upstart) and systemd have an "entirely" different approach where each service has a configuration file and the init system does all the magic.
To fully understand what's going on, one has to read and understand the init system and the quirks of the distribution in use.
* the process of initializing environment variables and starting a service.